foreach循环数组保证不会返回

时间:2019-04-22 17:46:49

标签: javascript firebase google-cloud-firestore

我想从“ mobile_user”下的给定ID访问一个名为“ followers”的子集合,其中包含关注者的ID。我想遍历followers子集合中的每个id,并从“ mobile_user”集合中查询数据。请注意,“关注者”子选项中的ID是“ mobile_user”下另一个用户的ID,其中包含我想要的文档数据。

我试过运气不好的诺言,我能够做foreach循环,只是查询名称作为测试,但是该数组正确填充,但是填充了N个用户名的数组是再也没有回来。

我的代码需要一些帮助,诺言使我发疯,我无法解决问题。

const getFollowers = (data, context) => {
        return new Promise((resolve, reject) => {

            let id = data.id
            const mobileUserRef = db.collection('mobile_user')

            return mobileUserRef.doc(id).collection('followers')
                .get()
                .then(function (doc) {
                    var result = []
                    doc.forEach(function (follower) {
                        mobileUserRef.doc(follower.id).get()
                            .then(function (followerdoc) {
                                result.push({
                                    name: followerdoc.data().name,
                                })
                                console.log(result)
                            })
                    })
                    return Promise.all(result);
                })
        });
    }

预期结果是一个数组,其中包含一个跟随者子下面的每个id的数据,如下所示:

在此示例中,仅存在2个用户ID

[ { name: 'Luis' }, { name: 'Marb Rocha' } ]

2 个答案:

答案 0 :(得分:1)

您需要在传递给Promise.all()的数组中添加一些promise,所以我认为您应该执行以下操作:

  const getFollowers = (data, context) => {
    let id = data.id;
    const mobileUserRef = db.collection('mobile_user');

    return mobileUserRef
      .doc(id)
      .collection('followers')
      .get()
      .then(querySnapshot => {
        var result = [];
        querySnapshot.forEach(follower => {
          result.push(mobileUserRef.doc(follower.id).get());
        });
        return Promise.all(result);
      })
      .then(results => {
        var finalResult = [];
        results.forEach(followerdoc => {
          finalResult.push({
            name: followerdoc.data().name
          });
        });
        return finalResult;
      });
  };

PS:不确定为什么要使用context参数。

答案 1 :(得分:1)

在调用result时,任何东西都不会被推送到return Promise.all(result);,因此,promise会在没有数据的情况下尽早解析。您可以在所有的诺言都解决后,利用链接返回结果,如下所示:

const getFollowers = (data, context) => {
    const results = [];

    const id = data.id
    const mobileUserRef = db.collection('mobile_user');

    //return the initial promise, and use chaining to *eventually* return the results
    return mobileUserRef.doc(id).collection('followers')
    .get()
    .then(doc=>{
        //use map to transform the doc results into an array of promises that call each user ref and load the doc
        const promises = doc.map(follower=>{
            return mobileUserRef.doc(follower.id).get()
            .then(followerdoc=>{
                //push items into the results
                results.push({
                    name: followerdoc.data().name,
                })
                return followerdoc;
            })
        })
        //execute all the promises, then return the results
        return Promise.all(promises).then(()=>{
            return results;//return the results, resolving our chain finally
        });
    })

}
相关问题