这是我的数据:
我想遍历 event_prods
中的每个 event_prod
并转到 eventGroups
子集合。进入该子集合后,我想遍历 eventGroups
中的每个 eventGroup
并获取文档数据。
到目前为止,这是我的代码:
async function getAllEventGroups() {
let eventGroups = []
try {
let eventProducerRef = await db.collection('event_prods')
let allEventProducers = eventProducerRef.get().then(
producer => {
producer.forEach(doc => console.log(doc.collection('eventGroups'))
}
)
} catch (error) {
console.log(`get(): there be an error ${error}`)
return []
}
return eventGroups
}
显然,它并没有按照我的意愿行事,但我不知道如何访问 eventGroups
子集合。在“doc”上调用“collection()”是未定义的。有人可以帮忙解决这个问题吗?顺便说一句,我不在乎这是否需要两个(或更多)查询,只要我不必引入我永远不会使用的数据即可。
编辑:这不是重复的,因为我知道我的子集的名称
最佳答案
您在 QueryDocumentSnapshot
上调用 .collection
.这种方法在那里不存在。但是作为 QueryDocumentSnapshot
延伸DocumentSnapshot
您可以对其调用 ref
以获取对所请求文档的引用。
```
let allEventProducers = eventProducerRef.get().then(
producer => {
producer.forEach(doc => console.log(doc.ref.collection('eventGroups')) // not the ref here
}
)
关于javascript - 在 Firestore 中获取嵌套文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51385112/