node.js - async waterfall callback isn't called within mongoose save -
i'm using express, mongoose, , async.
within update method on controller, i'm calling following:
//note: we're within constructor's prototype method, hence 'self'. body request body. async.waterfall([ function(callback) { self.collection.findbyid(body._id).exec(function(err, item) { if(item) { callback(null, item); } else { callback(err); } }); }, function(item, callback) { //callback(null, item); //makes task work, not need //merge 2 models together, save item.save( _.extend(item, body), function(err, item) { console.log('code here never seems called within waterfall'); callback(null, item); }); } ], function(err, results) { console.log('hello', err, results); self.res.json(results); });
so i'm trying find document id, merge new object in 1 found, save it, , return result json. callback function nested within .save never seems called. entire request seems hang , final function in waterfall never gets called.
i might wrong, seems save method's callback gets called asynchronously. how go getting work?
side note: if save method's callback async, why seem work?
var _this = this; var item = new this.collection(this.req.body); item.save(function(err, data) { _this.res.json(data); });
that method returns saved object json fine.
you not using model.save method correctly. takes callback first argument. must fetch item
instance, set new property values on item
instance, item.save(callback);
.
Comments
Post a Comment