Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我怎样才能得到“的”呢;“重新开始”;从该firebase集合查询函数中输出数组。我希望数组值用于另一个函数_Javascript_Firebase_Google Cloud Firestore - Fatal编程技术网

Javascript 我怎样才能得到“的”呢;“重新开始”;从该firebase集合查询函数中输出数组。我希望数组值用于另一个函数

Javascript 我怎样才能得到“的”呢;“重新开始”;从该firebase集合查询函数中输出数组。我希望数组值用于另一个函数,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,retArr具有存储在阵列中的每个城市的用户数。我需要在函数之外的其他地方使用这个数组。我怎样才能找回它 您可以这样做: db.collection("City").get().then(function(querySnapshot) { querySnapshot.forEach(function(doc) { db.collection("users").where("City", "==", doc.id).get().then(function(querySnapshot

retArr具有存储在阵列中的每个城市的用户数。我需要在函数之外的其他地方使用这个数组。我怎样才能找回它

您可以这样做:

db.collection("City").get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
    db.collection("users").where("City", "==", doc.id).get().then(function(querySnapshot) {
        var jgy_usercount = querySnapshot.size;
        retArr.push(jgy_usercount);
      });
    });
});
没有异步/等待:

async function getUsersPerDistrict() {
  const querySnapshot = await db.collection("district").get()
  const districts = []
  querySnapshot.forEach(x => { districts.push(x.data) })
  const districtCounts = await Promise.all(
    districts.map(async x => {
      const usersFromDistrict = await db.collection("users").where("City", "==", x.id).get()
      return { count: usersFromDistrict.size, name: x.name }
    })
  )

  return districtCounts
}

我没有测试它,只是从头顶上写下来。它肯定会有错误。然而,它应该为你指明正确的方向。
function getUsersPerDistrict() {
  return db.collection("district").get().then(querySnapshot => {
    const districts = []
    querySnapshot.forEach(x => { districts.push(x.data) })
    const districtCounts = Promise.all(
      districts.map(x => 
        db.collection("users").where("City", "==", x.id).get()
          .then(usersFromDistrict => ({
             count: usersFromDistrict.size,
             name: x.name
          }))
      )
    )
    return districtCounts
  }