0

I'm trying to get emails,firstname from a document and change a field in another one with my result, when i do a console.log(currentSenders) after my await i get all the data i need and when i try to push my result in the object, it push an empty array.

   await cars.forEach(async (car) => {
    await car.senders.slice(1).forEach(async (senderId) => {
      currentSender = await Meteor.users.findOne({ _id: senderId })
        currentSenders.push({
        email: currentSender.emails[0].address,
        firstname: currentSender.profile.name
      })
       console.log(currentSenders) // i get all the data i need
    })

     box.senders =  currentSenders //i want to insert the array currentSender into senders
    console.log(box) //the field box.senders is an empty array instead of being populated
  })
EyTa
  • 109
  • 3
  • 10

1 Answers1

0

forEach cannot be awaited in the way you wold expect, you have to do it in a good old for loop, see this answer: Using async/await with a forEach loop

A. Llorente
  • 1,142
  • 6
  • 16