I added the productArray array and need the data from products into that array. When I console.log it inside the loop it's working, but taking it out doesn't work. I am rendering it in ejs.
exports.getOrderById = (req, res, next) => {
let user = req.user
User.findById(user._id).exec((err, purchase)=>{
if(err){
return res.status(400).json('Error')
}
else {
let item = purchase.order
let productArray = []
item.forEach(function(items){
Product.findById(items.productId._id).exec((err, products)=>{
productArray.push({
productName: products.name,
productPrice: products.price
})
console.log(productArray[0].productName)
})
})
res.render('cart', {item: [item], products: [productArray]} )
}
})
};