我正在尝试获取子集合中的文档,该子集合是使用 .where 函数找到的文档的一部分
例子:
- 根目录/
- 文件 A/
- 子集 1
- 文档 1
- 文档 2
- 文档 3
- 子库 2
- 文档
- 子集 1
- 文件 A/
- 子集 1
- 文档 1
- 文档 2
- 文档 3
- 子库 2
- 文档
- 子集 1
我想从字段级别 == 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 的建议,我得到了同样的错误,“集合”不是函数
- > Current Code
- > Error
最佳答案
子集合位于特定文档下。您共享的查询现在指向 多个文档。您需要执行查询以确定它指向哪些文档,然后遍历结果,并获取每个文档的子集合。
在代码中:
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/