When I log my cartProducts within the forEach() loop, it successfully stores all the products. However, if I log my cartProducts outside of the loop, it displays an empty array.
var cartProducts = [];
const cart = await CartModel
.findOne({ UserDetailsId: userID })
.populate('UserDetailsId');
if (cart) {
cart.products.forEach(async(product) => {
const productItem = await ProductModel
.findOne({_id: product.productDetailsId });
cartProducts.push(productItem);
});
}
console.log("Cart Items", cartProducts);