javascript - 在 Firestore 中获取嵌套文档

标签 javascript firebase google-cloud-firestore

这是我的数据:

enter image description here

我想遍历 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/

相关文章:

Firebase Firestore 规则 : will get()'ing the document being checked counted as another read?

firebase - 数组类型将在Firestore文档中的字段转换为自定义对象列表

node.js - 用户在 Firebase/Node.js 中全局登录

firebase - 最佳实践 : Display data from Firebase in Flutter

java - Android Firebase db datasnapshot 从错误的数据库目录中提取信息

javascript - 如何使用jQuery在 Bootstrap 中动态设置弹出偏移

firebase - 如何根据 Firestore 数据库中的字段从集合中搜索文档?

javascript - TypeScript 通过 tsc 命令 : Output to single file without concatenation

java - 在 java jsoup 的 html 页面中搜索和查找变量值

javascript - 使用jquery从变量中删除textarea中的所有url