【发布时间】:2020-10-09 16:22:41
【问题描述】:
我有一个像这样的 firestore 对象:
{
countries:[
country1: {
cities: [
city1: {date_added: timestamp},
...
]
}
...
]
}
所以我希望在一个查询中 获得城市列表。我知道我可以去(我在 Firestore 功能中)
const cities = [];
admin.firestore().collection('countries/')
.get()
.then(countriesSnapshot => {
countriesSnapshot .forEach( countryDoc => {
admin.firestore().collection('countries/' + countryDoc.id + '/cities/')
.get()
.then(citySnapshot => {
citySnapshot.forEach( cityDoc => {
cities.push(cityDoc.id);
});
}).catch();
});
}).catch();
但这会进行双重 foreach,我只想在一切都解决后进行处理。我可以使用promise.all,但我想知道是否有更简单的方法——比如
admin.firestore().collection('countries/{countryId}/cities').get().then(citySnapshot ...
【问题讨论】:
标签: node.js firebase google-cloud-firestore google-cloud-functions firebase-admin