如何将Promise.all中的值传递给.then()?

时间:2019-09-08 18:58:19

标签: javascript firebase firebase-realtime-database promise

我有一个Promise链,该链首先收集联系人对象,然后处理收集的联系人。如何在Promise.all()之后将联系人传递到.then()部分?

let collectUserDataPromise = []
allUsers.forEach((userId) => {
  //Collect all contacts
  collectUserDataPromise.push(
    dbRoot.child(`users/${userId}`)
    .once('value', (userSnapshot)=>{
      const userNodeData = userSnapshot.val()
      const contactObject = {
        alias: (userNodeData.alias) ? userNodeData.alias : '',
        name: (userNodeData.name) ? userNodeData.name : '',
        status: 'active'
      }
      console.log('contactObject', contactObject)
      //return contactObject
      return new Promise((resolve, reject)=> resolve([contactObject]))
    })
  )
})

Promise.all(collectUserDataPromise)
.then((contactObjects)=>{
  console.log('contactObjects', contactObjects)

我的日志显示我已正确收集了对象,但是似乎无法传递给代码示例中最后一行的.then()部分。

.once()部分的日志

  

“ contactObject {别名:'​​Donald',名称:'Donald D',状态:'active'}”
  “ contactObject {别名:'​​Mickey',名称:'Mickey M',状态:'active'}”

.then()部分的日志

  

“ contactObjects [DataSnapshot {       节点_:        ChildrenNode {          children _:[对象],          priorityNode_:[Object],          indexMap _:[Object],          lazyHash_:null},       参考_:        参考{          回购:[对象],          路径:[对象],          queryParams_:[对象],          orderByCalled_:false},       index_:PriorityIndex {}},     DataSnapshot {       节点_:        ChildrenNode {          children _:[对象],          priorityNode_:[Object],          indexMap _:[Object],          lazyHash_:null},       参考_:        参考{          回购:[对象],          路径:[对象],          queryParams_:[对象],          orderByCalled_:false},       index_:PriorityIndex {}},     DataSnapshot {       节点_:        ChildrenNode {          children _:[对象],          priorityNode_:[Object],          indexMap _:[Object],          lazyHash_:null},       参考_:        参考{          回购:[对象],          路径:[对象],          queryParams_:[对象],          orderByCalled_:false},       index_:PriorityIndex {}}]“
   时间戳:“ 2019-09-08T18:50:40.259Z”
   跟踪:“ projects / xxxx / xxx”
  }“

如何将所有从Promise.all()收集的值传递给Promise链的.then()部分?

1 个答案:

答案 0 :(得分:1)

看起来您代码的dbRoot.child('...').once('...')部分实际上就是您要存储在collectUserDataPromise数组中的Promise。这意味着您无需像完成操作一样创建自己的Promise(return new Promise等)。

尝试以下方法,让我知道它会有所帮助:

let collectUserDataPromise = [];

allUsers.forEach(userId => {
  //Collect all contacts
  collectUserDataPromise.push(
    dbRoot
      .child(`users/${userId}`)
      .once("value")
      .then(userSnapshot => {
        const userNodeData = userSnapshot.val();
        const contactObject = {
          alias: userNodeData.alias ? userNodeData.alias : "",
          name: userNodeData.name ? userNodeData.name : "",
          status: "active"
        };
        //return contactObject
        return contactObject;
      })
  );
});

Promise.all(collectUserDataPromise).then(contactObjects => {
  console.log("contactObjects", contactObjects);
});