samedi 3 janvier 2015

Node.js: callback values depending on each other


Say I want to transform array with map function and each value is looked up in mongoDB with findOne, whose criteria in turn depends on current value from the array. To put it other way, simply transform array of ids to corresponding objects fetched from DB. Like:



arr.map(function(v) {
collection.findOne({_id: v}, function(
?
});

return {newField: ?};
});


Question marks are the places that need to be filled, but I guess the whole structure needs to be changed. Hopefully I'm clear.


I'm not used to such callback thinking and cannot wrap my head around it, am I missing something obvious?





Aucun commentaire:

Enregistrer un commentaire