0

I'm trying to return the updated document from mongodb but instead I'm console logging Null, any ideas why this might be happening? It's updating the document in the database, but it doesn't seem to want to return the full document to me so I can console log it.

User.findOneAndUpdate({userEmail}, {$set: {resetPasswordToken: token, resetPasswordExpires: now}}, function (err, res){
    console.log(res);
})
  • Does this answer your question? [Mongoose: findOneAndUpdate doesn't return updated document](https://stackoverflow.com/questions/32811510/mongoose-findoneandupdate-doesnt-return-updated-document) `.findOne...` would return `null` if there is no document exists in DB with given filter !! If there is one it will update the document & by default returns the old document instead of new one (For mongoose `{new : true}` will do the trick), But as you say document is getting updated then there is no chance of getting `null`.. – whoami - fakeFaceTrueSoul Jun 19 '20 at 19:37

1 Answers1

0

Use the {new: true} option.

User.findOneAndUpdate({
  email: userEmail
}, {
  $set: {
    resetPasswordToken: token,
    resetPasswordExpires: now
  }
}, {
  new: true
}, function(err, res) {
  console.log(res);
})
Lahiru Tennakoon
  • 621
  • 1
  • 6
  • 8