javascript - Firebase Cloud Firestore - How to query two sub-collections simultaneously?
问题描述
I want to query two sub-collections of a DocumentReference
.
I tried doing the following:
var promise1 = admin.firestore().collection(`players/${id}/collection1`).get();
var promise2 = admin.firestore().collection(`players/${id}/collection2`).get();
return Promise.all(promise1, promise2)
The problem though, is that by doing the following I get the following error:
TypeError: (var)[Symbol.iterator] is not a function at Function.all (native) at exports.onSubmission.functions.firestore.document.onCreate (/user_code/index.js:671:20)
So, I tried nesting them as below and it worked fine.
return admin.firestore().collection(`players/${id}/collection1`).get().then(foundersSnapshot => {
return admin.firestore().collection(`players/${id}/collection2`).get().then(metricsSnapshot => {
However, when deploying I get the warning saying
Avoid nesting promises
So, what are the best practices in this case? I would prefer doing through Promise.all
but it doesn't work. Also, it would output two collections as results in the then(results => {
So, in this case, could I safely regard results[0]
as "collection1" and `results[1] as "collection2"?
解决方案
Promise.all()
需要一个数组参数。注意周围的方括号promise1
,promise2
它们构成了一个内联数组:
Promise.all([promise1, promise2])
推荐阅读
- angular - 将图像添加到幻灯片(打字稿)
- html - 位置粘性无法正常工作,仅粘贴一小会儿
- java - datetimeoffset 列的正确 DateTimeFormatter 模式是什么
- php - php函数没有返回正确的值
- elasticsearch - ElasticSearch:关键字数据类型字段与值数组的完全匹配
- typescript - `rush build` 和 `tsc -b` 一起玩好吗?
- grafana - 将多个值减少为单个值
- python - 如果单元格值的年份小于特定年份,则删除 Excel 行
- json - 我需要帮助来确定现有 PostgreSQL 数据库中视图的语法,该数据库在 JSON 数组字段中提取(取消嵌套)内容
- c# - Unity列出敌人并将它们从所述列表中删除不起作用