异步添加后的空字段

时间:2018-06-05 13:24:53

标签: javascript

我尝试从文档中获取电子邮件,名字,并使用我的结果更改另一个字段中的字段, 当我在等待之后执行console.log(currentSenders)时,我得到了我需要的所有数据 当我尝试将结果推入对象时,它会推送一个空数组。

   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
  })

1 个答案:

答案 0 :(得分:0)

forEach无法以你期望的方式等待,你必须在一个好的旧循环中做到这一点,看到这个答案: Using async/await with a forEach loop