I have a character model which has an array of object id refs as the “inventory”. Each object id references an “equipment” with various stats. I want to get return an array that shows the stats, not the object ids. I thought I would forEach the array and findOne each of these equipments, then push their stats to the inventoryItems array.
Problem is this: Even though I can see with console.log that each item is being looked up successfully, when I log the array it comes out as empty.
Promise.all(promises).then(console.log(inventoryItems));
I put it in promises but that still doesn’t work. Why might this be happening?
let promises = [];
let inventoryItems = [];
Character.findOne({ user: req.user.id })
.then(character => {
character.inventory.forEach(item => {
promises.push(
EquipmentClass.findById(item._id).then(item => {
inventoryItems.push(item);
})
);
});
Promise.all(promises).then(console.log(inventoryItems));
})
.then(res.status(200).json({ inventory: "" }));
Repo:
I am coming across a similar problem in my code (which I haven’t fixed).
If I was doing this as a relational database you would have a link table to link items and users as it is a many to many relationship.
Here I am thinking have a “user inventory” collection having UserId and an array of Equipment items.
Not sure the best way to do this in MongoDb. 
The inventory, as part of a character in the characters collection is working. It properly holds an array of object ids with a reference to the equipment collection. The problem comes when trying to return not just the object ids, but the actual objects themselves. If I console log them in the forEach statement it shows the correct items. It is just not pushing them to a new array like I would want.
Thanks, I have added to my readme and I will try out this lookup object. I like it, much more clean way to do it.
To build this app: run npm install and npm install -D in both the client folder and the main (server) folder. The gulp and gulp sass are dev dependencies in the package json of the client folder.
I think I got the solution:
forgot to put anonymous callback function in the “then” after the promise.all. I also moved the res into the same “then”.
let promises = [];
let inventory = {
items: []
};
Character.findOne({ user: req.user.id }).then(character => {
character.inventory.forEach(item => {
promises.push(
EquipmentClass.findById(item._id).then(item => {
inventory.items.push(item);
})
);
});
Promise.all(promises).then(() => {
console.log("items:");
console.log(inventory);
res.status(200).json(inventory);
});