【问题标题】:How to use firebase functions with web app on front end? [closed]如何在前端的 Web 应用程序中使用 Firebase 功能? [关闭]
【发布时间】:2020-04-20 02:09:14
【问题描述】:

我想要检测更改是集合"users"。但并不是所有的变化都只有一种变化。我想在向集合中添加新文档时触发回调。

我对 Firebase 真的很陌生。我认为我需要的是 firebase 云功能。但是在文档中,我看不到任何在 HTML 中添加 firebase-functionsfirebase-tools 的方法。如果我们不能在没有节点的情况下使用 firebase-functions,那么请告诉正确的方法来检测在 firebase 中添加新文档...

注意:我也在使用打字稿,所以请考虑它不会抛出任何错误并给出正确的自动补全(如果有办法在浏览器上使用 firebase-functions)

【问题讨论】:

标签: javascript typescript firebase google-cloud-firestore


【解决方案1】:

在客户端收听集合的方法是使用onSnapshot。这将通知您每次更改,因此如果您只关心特定更改,您可以比较之前和之后的更改,并且仅在您关心的部分发生更改时运行您的代码。

let prevSnapshot;

firebase
  .app()
  .firestore()
  .collection('users')
  .onSnapshot(snapshot => {
    if (!prevSnapshot || snapshot.docs.length !== prevSnapshot.docs.length) {
      // this is either the first snapshot, or the number of documents has changed.
      prevSnapshot = snapshot;
      // Do whatever you need to do
    }
  });

【讨论】:

  • 如何在不遍历所有文档的情况下获取添加了哪个新文档,或者没有任何方法?
猜你喜欢
  • 2011-10-23
  • 1970-01-01
  • 2016-09-16
  • 2013-03-19
  • 1970-01-01
  • 2014-07-02
  • 2016-11-09
  • 1970-01-01
  • 2013-06-13
相关资源
最近更新 更多