无法使用猫鼬更改密码
Posted
技术标签:
【中文标题】无法使用猫鼬更改密码【英文标题】:unable to change password using mongoose 【发布时间】:2021-06-05 09:13:05 【问题描述】:我在我的网站上使用 MongoDB、mongoose、ejs 和 NodeJS。我有一个无法正常工作的更新密码功能。我通过在控制台中登录不同的东西一次又一次地检查,在req中获取数据没有问题。所以我想我的问题出在控制器上。这是我的控制器:
module.exports.update_password = function (req, res)
console.log(req.user);
Company.findOne( username: req.user.username , function (err, user)
if (user.password == req.body.current_password)
if (req.body.new_password == req.body.confirm_password)
Company.findOneAndUpdate( username: req.user.username , password: req.body.new_password , upsert: true , function (err, doc)
if (err) return res.send(500, error: err );
req.flash('notify', 'Password changed!')
return res.redirect('/profile');
);
else req.flash('notify', 'New password does not match with confirm password');
else req.flash('notify', '!')
);
return res.redirect('/profile');
每次更新我的密码时都会出现此错误:
node:events:355
throw er; // Unhandled 'error' event
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:329:5)
at ServerResponse.setHeader (node:_http_outgoing:573:11)
at ServerResponse.header (/home/krush/github/Project_Lightspeed/node_modules/express/lib/response.js:771:10)
at ServerResponse.location (/home/krush/github/Project_Lightspeed/node_modules/express/lib/response.js:888:15)
at ServerResponse.redirect (/home/krush/github/Project_Lightspeed/node_modules/express/lib/response.js:926:18)
at /home/krush/github/Project_Lightspeed/controllers/authentication_controller.js:45:32
at /home/krush/github/Project_Lightspeed/node_modules/mongoose/lib/model.js:4857:16
at /home/krush/github/Project_Lightspeed/node_modules/mongoose/lib/model.js:4857:16
at /home/krush/github/Project_Lightspeed/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
at /home/krush/github/Project_Lightspeed/node_modules/mongoose/lib/model.js:4880:21
at /home/krush/github/Project_Lightspeed/node_modules/mongoose/lib/query.js:4397:11
at /home/krush/github/Project_Lightspeed/node_modules/kareem/index.js:136:16
at processTicksAndRejections (node:internal/process/task_queues:76:11)
Emitted 'error' event on Function instance at:
at /home/krush/github/Project_Lightspeed/node_modules/mongoose/lib/model.js:4859:13
at /home/krush/github/Project_Lightspeed/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
[... lines matching original stack trace ...]
at processTicksAndRejections (node:internal/process/task_queues:76:11)
code: 'ERR_HTTP_HEADERS_SENT'
[nodemon] app crashed - waiting for file changes before starting...
【问题讨论】:
请查看:Under what circumstances may I add "urgent" or other similar phrases to my question, in order to obtain faster answers? 【参考方案1】:似乎问题在于return res.redirect('/profile');
命令在您的回调函数之前执行。这是正常的,因为这就是事件循环的工作方式。
尝试删除return res.redirect('/profile');
行以查看错误是否消失。 node.js 的回调在事件循环的后期执行。
通常我会建议重构您的代码以使用承诺而不是回调,因为这会导致“回调地狱”反模式。
如果您重构代码以使用 Promise,那么您将能够使用 .then 或 async await,这将帮助您编写更简洁的代码并帮助您轻松发现任何错误。
【讨论】:
非常感谢!!!那行得通。只是一个后续问题。如果我还想使用return res.redirect('/profile')
那我该怎么办?
正如我在之前的评论中提到的,最好的方法是使用 Promise 重构代码。如果您不想这样做,则应添加 return res.redirect('/profile');根据您要实现的业务逻辑,在您想要的所有 if else 块中声明。以上是关于无法使用猫鼬更改密码的主要内容,如果未能解决你的问题,请参考以下文章