Reputation:
I am new to using mongoose and I am having trouble updating a document. My code finds the document but does not update the property and ends up deleting the document. What am I doing wrong?
router.put("/hostGame/hostConfirm", (req, res, next) => {
GameModel.findOneAndUpdate(req.body.filter, req.body.update)
.then( game => {
console.log(game);
})
.catch(err => {
console.log(err)
res.send(err)
})
})
Upvotes: 0
Views: 47
Reputation: 24565
Per default the original document is returned from findOneAndUpdate. You need to enable the new
option to get the updated document:
GameModel.findOneAndUpdate(req.body.filter, req.body.update, {new: true})
Upvotes: 1