User is the basic schema I created with a name, email, and password. But when I try to update it no errors occur yet the collection in mongodb doesn't change. Tried the {strict: false} option on the schema but sadly that just adds another object to it, debbuging mode didn't help much either so now I am stuck with no clues how to go about solving this. Any advice as to what might be happening? mongoose version is 6.0.12 and mongodb is 4.4.10 if they are of relevance.
// Snippet for the code.
User.findOneAndUpdate({_id: req.body.id}, {updatedUser}, {useFindAndModify: false}, (err) => {
if(!err){
console.log("Managed to update")
res.json({message: "User updated successfully"})
} else {
res.json({message: `Something went wrong please try again => ${err} `})
}
})
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
Add doc
to callback function to see if you are getting updated document and this way you get better error messages.
// Snippet for the code.
User.findOneAndUpdate({_id: req.body.id}, {updatedUser}, {useFindAndModify: false}, (err, doc) => {
if(!err){
console.log("Managed to update")
console.log(doc)
res.json({message: "User updated successfully"})
} else {
res.json({message: `Something went wrong please try again => ${err} `})
}
})
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
_id
is usually and ObjectId, req.body.id
is usually string, so they don't match.
findOneAndUpdate
is successful if no matching document is found.