Mongo DB; Postman giving error even though user object successfully updates

Multi tool use


Mongo DB; Postman giving error even though user object successfully updates
Using Postman to update a user object in my Mongo DB on Mlab. The user object has an email, username, and password.
Here is the method in question that handles the PUT request:
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:
{
"password": "skittles"
}
The database on Mlab updates successfully, showing the new password.
However, Postman gives me the following error in its console:
{
"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);
, thinking that would help, but Postman is still giving me the error message.
return res.status(200).json(user);
Why am I getting this error when the user object successfully updates on the Mongo DB?
1 Answer
1
It's because of the ReferenceError
of note
variable.
ReferenceError
note
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 });
});
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.