javascript - 如何获取firestore中子集合的实时更新?
问题描述
我的数据库包含一个名为“已观看”的子集合,用于观看视频。我希望能够实时跟踪此类子集合。路径如下所示:users/userDocument/watched/date/nestedObjectsForMoviesWatched
例如users/as7f9as98dfa/watched/June-2020/
{name: Forrest Gump, watched: true} {name: Interstellar, watched: false}
这是我尝试处理的方法
useEffect(() => {
const unsubscribeWatched = firebase
.firestore()
.collection('users')
.doc(userId)
.collection('watched') //why doesn't this work?
.doc(month)
.onSnapshot(snapshot => {
//how to handle snapshot received?
})
return () => {
unsubscribeWatched()
}
}, [])
watched
当我的应用程序知道对给定对象中的属性进行更改时,这一点尤其重要。
解决方案
该文档似乎可以帮助您执行所需的任务。正如您在共享链接中所见,“如果您想在文档或查询元数据更改时接收快照事件,请在附加侦听器时传递侦听选项对象”。因此,只需在代码中添加“includeMetadataChanges: true”,如下所示。完整的例子可以在这里找到。
useEffect(() => {
const unsubscribeWatched = firebase
.firestore()
.collection('users')
.doc(userId)
.collection('watched')
.doc(month)
.onSnapshot(snapshot => {
includeMetadataChanges: true
})
return () => {
unsubscribeWatched()
}}
推荐阅读
- python - 在以下情况下,如何在第二个 for 循环中访问 (i+1 ) 索引?
- r - 每个集群的 FeatureScatter 的 facet_grid
- javascript - GetEnumSelectList 覆盖默认选择的选项值
- mathml - 使用 mathml 生成 eps 文件的 Wiris 演示
- sql-server - 如何让 MS Access 与 SQL Server 链接以根据先前的输入填充新输入
- haskell - 实例签名:对方法的约束
- python - 将列表转换为字符串以进行标记化
- python - 如何查找重复项并在文本文件中打印该行?
- reactive-programming - 我可以使用 AtomicReference 来获取 Mono 的值并且代码仍然保持反应性吗
- java - 在 GitHub 上发布所有矩阵构建版本的单元测试结果