Javascript 我怎样才能得到“的”呢;“重新开始”;从该firebase集合查询函数中输出数组。我希望数组值用于另一个函数
retArr具有存储在阵列中的每个城市的用户数。我需要在函数之外的其他地方使用这个数组。我怎样才能找回它 您可以这样做: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
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
}