猿问

NodeJS:更改密码期间未处理的承诺拒绝警告

我正在 NodeJS 中更改密码,但在请求处理过程中出现以下错误:


(node:16220) UnhandledPromiseRejectionWarning: Error: Can't set headers after they are sent.

    at validateHeader (_http_outgoing.js:491:11)

    at ServerResponse.setHeader (_http_outgoing.js:498:3)

    at ServerResponse.header (/home/pbaj/Documents/Projects/syberiaquotes-backend/node_modules/express/lib/response.js:771:10)

    at ServerResponse.send (/home/pbaj/Documents/Projects/syberiaquotes-backend/node_modules/express/lib/response.js:170:12)

    at ServerResponse.json (/home/pbaj/Documents/Projects/syberiaquotes-backend/node_modules/express/lib/response.js:267:15)

    at user.(anonymous function).updateOne.then.catch.err (/home/pbaj/Documents/Projects/syberiaquotes-backend/api/controllers/user.js:284:52)

    at <anonymous>

    at process._tickCallback (internal/process/next_tick.js:188:7)

(node:16220) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)

(node:16220) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

在我的changePassword路线中,我采用email,password,newPassword价值观和:

  1. 检查电子邮件是否存在,如果存在...

  2. 比较给定的密码和用户密码,如果它们相同...

  3. bcrypt.hash 函数采用 newPassword,从中生成哈希值,并保存到 mongoDB

如果我提供正确的电子邮件和密码,上面的所有要点都已完成,但我明白了UnhandledPromiseRejectionWarning

我看到这个问题的原因是在user[0].updateOne(user[0])函数内部,正是在,catch()但我不知道发生了什么。

我需要[200]'Password changed!'从我的要求的答案,但我得到[401]'Auth failed'becouse的UnhandledPromiseRejectionWarning


慕桂英546537
浏览 386回答 2
2回答

精慕HU

您发送响应 2 次。试试这个代码&nbsp;if (result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bcrypt.hash(newPassword, 10, (err, hash) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(hash)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (err) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return res.status(500).json({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; error: err&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user[0].password = hash&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; user[0]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .updateOne(user[0])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .then(result => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return res.status(200).json({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: 'Password changed!',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result: result,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .catch(err => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res.status(500).json({ message: err.message })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })} else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return res.status(401).json({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: 'Auth failed'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })}

慕莱坞森

我假设您使用的是猫鼬(因为User.find(...))。如果是这样的话,你不能这样做user[0].updateOne(...),因为user[0]已经是一个类型的对象User。您要么执行User.findOneAndUpdate(...),要么user[0].save(...)在设置新密码后执行(这对我来说似乎更直接)。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答