router.get("/api/cart", auth, async (req, res) => {
try {
const user = await User.findById(req.user._id);
items = [];
await user.cartProducts.forEach(async (product) => {
var item = await Item.findById(product._id);
items.push(item);
console.log(items);
});
console.log(items)
res.send(items);
} catch (e) {
res.status(500).send(e);
}
});
I am retrieving the products selected by the user and sending them back in an array. The first console log displays the array with the products, but the second one shows an empty array. The API is functioning correctly without any issues. I suspect that the problem lies in my understanding of JavaScript concepts.