javascript - 在 Firestore 中获取文档集合
问题描述
这是我的 Firebase 数据库
- conversations (collection)
-- xxx (document)
--- users (collection)
---- xxx (document)
我想列出所有对话及其用户
this.db.collection('conversations').get().then(querySnapshot => {
querySnapshot.docs.forEach(doc => {
console.log(doc.collection('users').get())
});
});
我得到 doc.collection 不是函数
更新:这是我在获得 id 并进行新查询后所拥有的。现在的问题是。这是高性能的吗?
this.db.collection('conversations').get().then(conversationsQuerySnapshot => {
conversationsQuerySnapshot.docs.forEach(doc => {
this.db.collection('conversations').doc(doc.id).collection('users').get().then(usersQuerySnapshot => {
usersQuerySnapshot.docs.forEach(doc => {
console.table(doc.data());
});
});
});
});
解决方案
您正在寻找doc.ref.collection('users')
,因为您需要从DocumentSnapshot
到其DocumentReference
通过doc.ref
。
请注意,我发现通过简单地遵循类型最容易发现此类错误。你querySnapshot
是一个QuerySnapshot
,这意味着你doc
是一个QueryDocumentSnapshot
。由于QueryDocumentSnapshot
没有collection
方法,因此更容易找出问题所在。
推荐阅读
- javascript - JavaScript 中不按按钮的自动代码操作
- c# - Caching Serialized HttpResponseMessage to Redis. Error on read. "InvalidOperationException: The stream was already consumed. It cannot be read again."
- reactjs - Graphql,React - TypeError:无法读取未定义的属性“地图”
- i18next - 如何使用 CLI 使用 i18next 生成输出 json 文件?
- html - Angular page jumps to top when clicking anywhere at the bottom
- python - 将大型多维列表写入CSV中的单个单元格
- python-3.x - 无法连接到面临失败的 PYMQI 客户端:MQRC_ENVIRONMENT_ERROR
- android - Koin 构造函数注入少量参数 MVVM Android 架构
- typescript - 有没有一种合理的方法可以在打字稿中输入“小于 n 的非负整数”?
- firebase - Firestore 侦听器是否仅传输更改的数据?