gpt4 book ai didi

javascript - Firestore : Get subcollection of document found with where

转载 作者:行者123 更新时间:2023-11-30 07:20:33 33 4
gpt4 key购买 nike

我正在尝试获取子集合中的文档,该子集合是使用 .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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com