user11580406
user11580406

Reputation:

Why is findOneAndUpdate() not updaing the document?

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

Answers (1)

eol
eol

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

Related Questions