[英]Mongo DB; Postman giving error even though user object successfully updates
Using Postman to update a user object in my Mongo DB on Mlab. 使用Postman更新我在Mlab上的Mongo数据库中的用户对象。 The user object has an email, username, and password.
用户对象具有电子邮件,用户名和密码。
Here is the method in question that handles the PUT request: 这是处理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 });
});
});
When I make a request with Postman entering in the following JSON object to update my user's password: 当我通过Postman发出请求时,输入以下JSON对象以更新用户密码:
{
"password": "skittles"
}
The database on Mlab updates successfully, showing the new password. Mlab上的数据库成功更新,显示新密码。
However, Postman gives me the following error in its console: 但是,Postman在其控制台中给我以下错误:
{
"message": "There was a problem finding that user",
"error": {}
}
I thought that maybe this was due to the rest of the code continuing to execute after updating the object, so I added a return in the return res.status(200).json(user);
我以为这可能是由于更新对象后其余代码继续执行,所以我在
return res.status(200).json(user);
添加了return return res.status(200).json(user);
, thinking that would help, but Postman is still giving me the error message. ,以为这会有所帮助,但Postman仍然给我错误消息。
Why am I getting this error when the user object successfully updates on the Mongo DB? 当用户对象在Mongo DB上成功更新时,为什么会出现此错误?
It's because of the ReferenceError
of note
variable. 这是因为
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 });
});
In future, if something comes to catch
block, print it with the console.log
. 在未来,如果事情来
catch
块,用打印console.log
。 Because, you can't send it with .json()
. 因为,您不能使用
.json()
发送它。
If you want to know the error in the response, try this, 如果您想知道响应中的错误,请尝试此操作,
res.json({
message: 'something',
error: (err && err.message) || 'Not available',
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.