node.js - mongoose subdocuments don't reflect update after Model.findByIdAndUpdate -
so i've got schema looks like:
var projectschema = new mongoose.schema({ scrum_master_id: {type: mongoose.schema.types.objectid, ref: 'user'}, developers: [{type: mongoose.schema.types.objectid, ref: 'developer'}], scrums: [{type: mongoose.schema.types.objectid, ref: 'scrum'}], created_at: {type: date, default: date.now}, meta: { trello_board_id: string, basecamp_url: string }, description: string, title: string });
i can send express server , update properly. route in question looks like:
.put(jwt.protect, function (req, res) { project.findbyidandupdate(req.params.id, {$set: req.body}, function (err, project) { console.log(project); res.json(project); }); })
the issue have sub documents developers aren't updated in project
variable being passed findbyidandupdate callback. documents updated in database, not in callback of update function. how can refresh sub documents reflect database has?
findbyidandupdate
takes options parameter can pass {new:true}
, it'll return modified document:
project.findbyidandupdate(req.params.id, {$set: req.body}, {new: true}, function (err, project) { console.log(project); res.json(project); });
Comments
Post a Comment