Javascript Mongo DB;即使用户对象成功更新,邮递员仍给出错误

Javascript Mongo DB;即使用户对象成功更新,邮递员仍给出错误,javascript,mongodb,mongoose,postman,Javascript,Mongodb,Mongoose,Postman,使用Postman在Mlab上更新我的Mongo DB中的用户对象。用户对象具有电子邮件、用户名和密码 以下是处理PUT请求的方法: server.put('/edit/:id', (req, res) => { const { id } = req.params; const changes = req.body; const options = { new: true, }; User.findByIdAndUpdate(id

使用Postman在Mlab上更新我的Mongo DB中的用户对象。用户对象具有电子邮件、用户名和密码

以下是处理PUT请求的方法:

server.put('/edit/:id', (req, res) => {
    const { id } = req.params;
    const changes = req.body;

    const options = {
        new: true,
    };

    User.findByIdAndUpdate(id, changes, options)
        .then(user => {
            if (note) {
                return res.status(200).json(user);
            } else {
                res.status(404).json({ message: 'User not found' });
            }
        })
        .catch(err => {
            res
                .status(500)
                .json({ message: 'There was a problem finding that user', error: err });
        });
});
当我请求Postman输入以下JSON对象以更新用户密码时:

{
  "password": "skittles"
}
Mlab上的数据库更新成功,显示新密码

但是,Postman在其控制台中给了我以下错误:

{
    "message": "There was a problem finding that user",
    "error": {}
} 
我认为这可能是因为在更新对象之后,其余的代码继续执行,所以我在
return res.status(200.json)(user)中添加了一个return,认为这会有帮助,但邮递员仍然给我错误消息


当用户对象在Mongo DB上成功更新时,为什么会出现此错误?

这是因为
note
变量的
ReferenceError

User.findByIdAndUpdate(id, changes, options)
        .then(user => {
            if (user) {
                return res.status(200).json(user);
            } else {
                res.status(404).json({ message: 'User not found' });
            }
        })
        .catch(err => {
            res
                .status(500)
                .json({ message: 'There was a problem finding that user', error: err });
        });
将来,如果出现
catch
块,请使用
console.log
打印它。因为,您不能使用
.json()
发送它

如果您想知道响应中的错误,请尝试以下操作

res.json({
   message: 'something',
   error: (err && err.message) || 'Not available',
})

在学习编程三年后,仍然在最简单的事情上结结巴巴。这就解决了问题,非常感谢。事情发生了,冷静。只要有经验:)