无法使用猫鼬更改密码

unable to change password using mongoose

我正在为我的站点使用 MongoDB、mongoose、ejs 和 NodeJS。我有一个不能正常工作的更新密码功能。我通过在控制台中登录不同的东西一次又一次地检查,并且在请求中获取数据没有问题。所以我想我的问题出在控制器上。这是我的控制器:

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...

看来问题是 return res.redirect('/profile'); 命令在回调函数之前执行。这是正常的,因为这是事件循环的工作方式。

尝试删除 return res.redirect('/profile'); 行以查看错误是否消失。 node.js 处的回调在事件循环的后期执行。

通常我建议重构您的代码以使用承诺而不是回调,因为这会导致“回调地狱”反模式。

如果您重构代码以使用 promises,那么您将能够使用 .then 或 async await,这将帮助您编写更清晰的代码并帮助您轻松发现任何错误。