Using mongoose and Q.spread gives array callback parameters Using mongoose and Q.spread gives array callback parameters mongoose mongoose

Using mongoose and Q.spread gives array callback parameters


The save callback signature is function(err, result, numberAffected) which doesn't conform to node callback convention. nfcall expects a node callback signature, which is function(err, result). To avoid loss of information, the promise returned by nfcall resolves to [result, numberAffected].

Using .bind and Q.nfcall at call sites is very ugly anyway, so you can create a method that does all this:

mongoose.Model.prototype.saveForResult = function() {    return Q.nbind(this.save, this)().spread(function(result, numberAffected) {        return result;    });};

Then:

Q.spread([    employee.saveForResult(),    dept.saveForResult()],function(emp,dept){    console.log(JSON.stringify(emp));    console.log(JSON.stringify(dept));    mongoose.disconnect();})