After implementing async/await with rawCollection for data retrieval within a loop, I noticed that setting new properties (poTmp) works as expected. However, once I set the new properties and try to console.log outside of the loop, the new props are not being set. Why is this happening?
let items = await Items.rawCollection()
.aggregate([
{
$match: match,
},
])
.toArray()
let data = items
data.forEach(async item => {
let po = await PurchaseOrderDetails.rawCollection()
.aggregate([
{
$match: {
itemId: item._id,
tranDate: { $lte: tDate },
},
},
{
$group: {
_id: '$itemId',
itemDoc: { $last: item },
onHandPO: { $sum: '$qtyBase' },
},
},
{
$group: {
_id: '$itemId',
itemDoc: { $last: '$itemDoc' },
lastOnHandPO: { $last: '$onHandPO' },
},
},
])
.toArray()
//==================
//set new properties
//==================
item.poTmp = po[0]
})
console.log(data)
return data