How can i get access to the updated object if its not coming in through the callback?

Im using mongoose findByIdAndUpdate, and everything is updating accordingly, but my callback doesnt return the updated object. Im getting the old object values from before the update. Its not until i fire update a second time that i can see the updated object.
How can i get access to the updated object if its not coming in through the callback?
 
router.put('/', (req,res) => {
const teacherId = req.body.teacherId;

if(teacherId) {
Teacher.findByIdAndUpdate(teacherId, {
$set: {
name: req.body.name,
email: req.body.email,
instrument: req.body.instrument
}}, function(err, data) {
if(err) console.error(err);

return res.status(200).json({
data // <--- doesnt have the new values
});

});
}
});
You already invited:

rogers

Upvotes from:

You need to specify after the update {returnNewDocument: true}
 
Teacher.findByIdAndUpdate(teacherId, {
$set: {
name: req.body.name,
email: req.body.email,
instrument: req.body.instrument
}, {
returnNewDocument: true
}}
Actually, thats for mongo, looks like with mongoose you just do {new: true}

If you wanna answer this question please Login or Register