Categories
JavaScript Answers

How to fix Mongoose findByIdAndUpdate not returning correct value?

Spread the love

Sometimes, we want to fix Mongoose findByIdAndUpdate not returning correct value.

In this article, we’ll look at how to fix Mongoose findByIdAndUpdate not returning correct value.

How to fix Mongoose findByIdAndUpdate not returning correct value?

To fix Mongoose findByIdAndUpdate not returning correct value, we call findByIdAndUpdate with the new option set to true.

For instance, we write:

Model.findByIdAndUpdate(id, updateObj, {
  new: true
}, (err, model) => {
  //...
})

to call findByIdAndUpdate with the id of the item to update, updateObj with the new data for the item, an object with new set to true, and the callback that runs when the update is done.

We set new to true to return the new document value as the value of model in the callback.

Conclusion

To fix Mongoose findByIdAndUpdate not returning correct value, we call findByIdAndUpdate with the new option set to true.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *