In my API while POST the route , i have to find the list of products which current user have in his Cart. I am having current user details then whenever i try to apply forEach loop on cart to extract information from my Products collection and pushing that info to new array and printing that new array, it is showing empty. while printing it inside forEach shows the data. How do i fix it so it also prints the new array data?
BuyerRoute.post("/Orders", verifyToken,async(req,res) =>{
let buyer = await Buyer.findById(res.current_user.buyer_user._id)
var e = []
buyer.Buyer_Cart.forEach( async (prodObj) => {
let products = await Products.findById(prodObj.Product_ID)
e.push(products)
})
console.log(e)
})
Output: []
While inside the forEach loop
BuyerRoute.post("/Orders", verifyToken,async(req,res) =>{
let buyer = await Buyer.findById(res.current_user.buyer_user._id)
var e = []
buyer.Buyer_Cart.forEach( async (prodObj) => {
let products = await Products.findById(prodObj.Product_ID)
e.push(products)
console.log(e)
})})
Output:
[
{
_id: new ObjectId("6381b9b4c5efd30fe8492902"),
Product_name: 'headphones',
Product_description: 'headphones for PC',
Product_price: 500,
Quantity_available: 10,
seller_Id: '6381b7f3fdc04885a28723c1',
seller_name: 'xyz',
__v: 0
}
]