javascript - Firestore : Get subcollection of document found with where

标签 javascript firebase google-cloud-platform google-cloud-firestore typeerror

我正在尝试获取子集合中的文档,该子集合是使用 .where 函数找到的文档的一部分

例子:

  • 根目录/
  • 文件 A/
    • 子集 1
      • 文档 1
      • 文档 2
      • 文档 3
    • 子库 2
      • 文档
  • 文件 A/
    • 子集 1
      • 文档 1
      • 文档 2
      • 文档 3
    • 子库 2
      • 文档

我想从字段级别 == 1 的文档中获取 SubColl 1 下的所有文档

我正在尝试这样做:

db.collection("RootColl").where("field", "==", "1").collection("SubColl 1").get()

但是这样做我得到了错误

Uncaught TypeError: db.collection(...).where(...).collection is not a function

编辑 1: 按照 Frank van Puffelen 的建议,我得到了同样的错误,“集合”不是函数

最佳答案

子集合位于特定文档下。您共享的查询现在指向 多个文档。您需要执行查询以确定它指向哪些文档,然后遍历结果,并获取每个文档的子集合。

在代码中:

var query = db.collection("RootColl").where("field", "==", "1");
query.get().then((querySnapshot) => {
  querySnapshot.forEach((document) => {
    document.ref.collection("SubColl 1").get().then((querySnapshot) => {
      ...
    });
  });
});

关于javascript - Firestore : Get subcollection of document found with where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47634907/

相关文章:

javascript - startsWith 不是无缘无故被调用的函数

javascript - 将行添加到 jqGrid 中,无需重新加载页面

python - 大查询存储。 Python。并行读取多个流(多处理)

google-cloud-platform - 可以通过 Terraform 为 Cloud Function 设置 secret 吗?

google-cloud-platform - gcloud beta 项目移动 some-project-id --organization=0

JavaScript 函数长度和参数属性/字段

javascript - jQuery text() 方法加载

ios - 如何根据segmentedControl的选定索引更改刷新器的功能

javascript - Cloud Functions for Firebase HTTP 超时

flutter - Firebase Firestore 查询