node.js - Mongoose $addToSet return new list entrys -
i have question working mongoose 4.0.1
i trying add new picture objects array inside model. code of endpoint doing job:
// add new images exports.pictures = function(req, res) { incident.findbyidandupdate( req.params.id, {$addtoset: {"pictures": {$each: req.body}}}, {new: true}, function(err, incident) { if (err) { return handleerror(res, err); } return res.send(201).json(incident.pictures); } ); }; the problem: callback object (incident) stores information of model found , updated. want return new array entries created.
how can receive actual changes of operation instead of whole object storing pictures array?
i solved problem creating new schema pictures , adding reference incident model.
the endpoint changed well:
- create new picture instances array of pictures
- find incident id
- save references of picture instances array inside incident
- return id of picture instances
var _ = require('lodash'); // add new images exports.pictures = function(req, res) { picture.create(req.body, function(err, pictures) { if (err) { return handleerror(res, err); } incident.findbyidandupdate( req.params.id, {$addtoset: {"pictures": {$each: pictures}}}, function(err) { if (err) { return handleerror(res, err); } var pictureids = _.map(pictures, '_id'); return res.status(201).json(pictureids); } ); }); };
Comments
Post a Comment