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?
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
});
});
}
});
No any search results
You already invited:
1 Answers
rogers
Upvotes from:
Teacher.findByIdAndUpdate(teacherId, {Actually, thats for mongo, looks like with mongoose you just do {new: true}$set: {
name: req.body.name,
email: req.body.email,
instrument: req.body.instrument
}, {
returnNewDocument: true
}}