I try get 3 documents with IDs: 1, 100, 1000. But node.js is async, and array objects in result is empty. How put documents into array?
var objects = {};
var objectsID = [1, 100, 1000];
objectsID.forEach( function( elem ){
objectsDB.get( elem, function( err, object ){
objects[elem] = object;
})
});
console.log(objects);
console.log returns {}.
So this is a classical asynchronous code issue. Your code can be rewritten like this:
You might want to have a look at Caolan’s async.js library as well ( in particular async.parallel ).