【问题标题】:How to get only updated document from Firestore如何从 Firestore 仅获取更新的文档
【发布时间】:2018-08-21 01:20:46
【问题描述】:

有没有办法在使用 valueChanges 时只从 firestore 获取更新的文档而不是所有文档

return this.afs.collection('users').doc(userId).collection('chat_users').valueChanges();

我有以下代码,当只有一个文档发生变化时,它会返回所有文档。

【问题讨论】:

    标签: angular google-cloud-firestore angularfire2


    【解决方案1】:

    您可以为此使用stateChanges()

    它是什么? - 以 DocumentChangeAction[] 的形式返回最新更改的 Observable。

    你为什么要使用它? - 上述方法返回一个按查询顺序排序的同步数组。 stateChanges() 在更改发生时发出更改,而不是同步查询顺序。 ...

    return this.afs.collection('users')
        .doc(userId)
        .collection('chat_users')
        .stateChanges(['modified'])
        .pipe(
            map((actions) => actions.map(a => {
                const data = a.payload.doc.data();
                const id = a.payload.doc.id;
                return { id, ...data };
            }))
        );
    

    【讨论】:

      猜你喜欢
      • 2021-05-15
      • 2021-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 2020-12-05
      • 1970-01-01
      相关资源
      最近更新 更多