Question

I am trying to put a number of documents from a MongoDB collection into an array, using node.js&mongoose. Logging the userDoc in the _.each-loop works fine, but not appending them to an array.

What am I doing wrong?
My best guess is that I have misunderstood something regarding node's asynchronous design, but I have no idea on what I should change.

The code with comments:

returnObject.list = [];

Users.find({}, function (err, user){

    _.each(user, function(userDoc){             
        console.log(userDoc); // Works
        returnObject.list.push(userDoc); // No errors, but no users appended
    });

});


console.log(returnObject); // No users here!

res.send(JSON.stringify(returnObject)); // Aint no users here either!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top