【发布时间】:2020-01-17 18:43:07
【问题描述】:
我正在尝试为我的应用做这样的事情:
snapshotListeners() {
firestore()
.collectionGroup()
.where()
.onSnapshot({
error: //Handle error
next: firstSnapshot => {
firstSnapshot.docsChanges().forEach(change => {
//Data retrieved and used in the below query to get more data
firestore()
.collection()
.where() //using the data retrived from the above query here.
.onSnapshot({
error: //Handle error
next: secondSnapshot => {
secondSnapshot.docsChanges().forEach(change =>{
//More Data from second query
})
}
})
})
}
})
}
第二个查询依赖于从第一个查询中检索到的数据,我希望在各自文档中监听两个查询的变化。
例如, 组功能: 这是我的数据库结构:
Groups(top-level collection)
|_groupId(documents) - Members(subcollection)
|_groupId | |_uid(document)
| |_uid
|
|__groupdata(Fields like title, description, etc)
所以我将使用first listener 来监听成员子集合中特定用户的文档的更改,并使用second listener 来实时检索他所在的组(关于组的数据),这样当他被添加/从组中删除,列表/前端会像 WhatsApp 一样自动更新。
或者即使组数据发生了变化,如标题、描述等,这些变化也会在前端听到并更新。
对于此用例,是否建议使用这种嵌套的快照侦听器,还是有其他方法?
非常感谢您的帮助。
【问题讨论】:
标签: javascript firebase react-native google-cloud-firestore react-native-firebase