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:

  1. create new picture instances array of pictures
  2. find incident id
  3. save references of picture instances array inside incident
  4. 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

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -