I'm working on a NestJSxMongoose store management project.
I have this piece of code in which I want to update some items in the database and put those updated items in an array that I will use later.
const updatedItems: Item[] = [];
purchaseData.items.forEach(async (purchasedItem) => {
  const itemInDB = await this.itemService.findItemByName(purchasedItem.name);
  itemInDB.quantity -= purchasedItem.quantity;
  const updatedItem = await this.itemService.updateItem(
    itemInDB['_id'],
    itemInDB
  );
  updatedItems.push(updatedItem);
  console.log(updatedItems); // Output : [{actual_data}]
});
console.log(updatedItems); // Output : []
The issue I have is that when I log the content of updatedItems inside the forEach(), it contains the actual expected data. But when I try to use it outside the forEach() loop, it logs an empty array. I want to use that array outside of the forEach().
What am I doing wrong ?
 
     
     
     
    