mercredi 18 février 2015

mongoose exec call with async.parallel

I have a code like this in my express controller



function (req, res) {
var queries = [];

data.forEach(function (item) {
var query = myModel.findOneAndUpdate({remoteId: item.id}, item, {upsert: true}).exec;

queries.push(query);
});

async.parallel(queries, function (err, docs) {
res.json(docs);
});
});


If data array has 3 item, then i have an array of 3 null values.


async.parallel function accepts a function with a callback argument, that should be called to properly complete its execution. So mongoose.Query.exec does the same. But i recieve an array of null objects as a result.


If i wrap my exec call like so



var query = function (cb) {
tournamentsModel.findOneAndUpdate({remoteId: item.id}, item, {upsert: true}).exec(function (err, model) {
cb(err, model);
});
};
queries.push(query);


everything is ok and i recieve 3 docs from mongo as a result. Why should i explicitly call a callback passed to a async.parallel function call, when exec method does the same?


Aucun commentaire:

Enregistrer un commentaire