I have some code like this:
req.models.order.find({})
.each(function(order){
req.models.food.find({orderId: order.id}{
//...
order.foods = ['...'];
return order;
})
})
.get(function(order){
//the orders have no 'foods' attribute
})
How could I fix it?
First of all, you can use associations (like one-to-many or many-to-many), which would be the most preferable path to go.
The other way to do it (maybe not too pro, but surely works) is to make a counter initialized with number of returned orders, decrementing with every next "each" iteration, and eventually firing "get" inside the "each" when the counter reaches 0.