0

Below is the demo function to show how I encountered a problem when using forEach to combine the data into the object.

async function fetcher(){
    var testObject = {}

    [1,2,3].forEach(async (num) => {
    testObject[num] = await db.collection.find(name: num)
})
    console.log(testObject)
}

fetcher()

Empty object {} is logged, may I ask why?

TungTung
  • 163
  • 7

0 Answers0