Javascript 未返回foreach循环数组承诺

Javascript 未返回foreach循环数组承诺,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,我想从“mobile_user”下的给定id访问名为“followers”的子集合,其中包含追随者的id。我想循环查看followers子集合中的每个id,并查询“mobile_user”集合中的数据。请注意,“followers”子集合中的id是“mobile_user”下另一个用户的id,其中包含我想要的文档数据 我曾经尝试过在承诺中捣乱,但运气不好,我能够执行foreach循环,并只是作为测试查询名称,但是数组被正确填充,但是包含N个用户名称的填充数组永远不会返回 我需要一些代码方面的帮助

我想从“mobile_user”下的给定id访问名为“followers”的子集合,其中包含追随者的id。我想循环查看followers子集合中的每个id,并查询“mobile_user”集合中的数据。请注意,“followers”子集合中的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);
                })
        });
    }
预期结果是一个数组,其中包含followers子项下每个id的数据,如下所示:

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

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

您需要向传递给的数组添加一些承诺,因此我认为您应该执行以下操作:

  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:不确定您为什么有一个
上下文
参数。

返回承诺时,没有任何内容被推送到
结果,因此承诺将在没有数据的情况下提前解析。一旦所有承诺得到解决,您可以利用链接返回结果,如下所示:

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

}

在与控制台相同的位置进行回位。log@NabeelKhan我还是有插座挂断谢谢,这很有帮助。虽然当我尝试运行它时,我得到了错误
未处理的错误类型error:doc.map不是一个函数
@Louis,它似乎表明
doc
不是一个
数组
,或者代码运行的环境没有
数组.prototype.map
方法,我相信这是在es5中添加的,因此,它可能并非在所有环境中都可用。要提出修复建议,我需要更多地了解环境。谢谢,数组位于doc.docs.map下,我现在得到了我想要的结果。我爱you@Louis没问题,很高兴你弄明白了