简体   繁体   English

我如何从此Firebase集合查询功能中获取“ retArr”数组。 我想将数组值用于其他功能

[英]How do i get the “retArr” array out from this firebase collection query function. I want the array values to used for another function

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

The retArr has the number of users per city that is stored in the array. retArr具有存储在数组中的每个城市的用户数。 this array i need to use elsewhere out of the function. 我需要在功能之外的其他地方使用此数组。 How can retrieve it? 如何找回它?

You could do something like this: 您可以执行以下操作:

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
}

Without async/await: 没有异步/等待:

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
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 function 中获取数组? - How do I get an array out of a function? 如何从异步函数中获取数组的所有值 - How do I get all the values of an array from an async function 如何在另一个函数中的函数内部使用数组中的值? - how do I use values from an array inside a function in another function? 我希望将 javascript 中该函数的函数名称和主体解析为一个单独的文件。 我如何使用 Java 做到这一点? - I want the function name and body of that function from a javascript to get parsed out into a separate file. How do I do that using Java? 如何将函数返回的数组复制到 JavaScript 中的另一个数组中? - How do I copy an array returned by a function into another array in JavaScript? 如何将数组转换为另一个函数,以便将其显示到另一个div中 - How do I get an array to another function so I can display it into another div 如何从数组中获取值? - How do I get values from an array? 在Ajax中,我希望具有2个或更多输入功能。 我怎样才能做到这一点? - In Ajax, I want to have 2 and more input function. How Can I do this? 如何将此php函数转换为jquery函数。 它从数组返回值的所有可能组合。 - how to convert this php function to jquery function. It returns every possible combination of the values from an array. 如何调用我从 JavaScript 中的数组中提取的函数? - How do I call the function I pull out from an array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM