【问题标题】:Ionic3 Angularfire listen to firestore changesIonic3 Angularfire 监听 Firestore 的变化
【发布时间】:2019-02-17 18:53:13
【问题描述】:

firebase 云功能成功更新文档后如何监听事件?因为函数运行时有几秒钟的延迟。我想实现一个加载屏幕并在函数成功更新记录时将其关闭。

示例:

  1. MyApp 将数据更新到 firebase ref A
  2. firebase 函数检测 ref A 的变化并执行更新相同的数据到 ref B
  3. MyApp 不知道 firebase 函数何时会完成上述第 2 步,我需要听 ref B 进行更改以解除加载

【问题讨论】:

    标签: angular ionic3 google-cloud-firestore google-cloud-functions


    【解决方案1】:

    您可以使用 Angularfire 的 updateset 方法来更改文档的值。这些方法返回一个Promise,完成后你可以删除hte loader。

    updateItem() {
        this.loading = true;
        const item = { id: 1, name: "My Item };
        this.db.doc('item/1').update(item)
                             .then(() => this.loading = false;;
    }
    

    更多例子请看angularfire documents documentation

    更新:

    如果您需要订阅文档更改而不更改它,只需执行以下操作:

    this.db.doc('item/1).valueChanges()
                        .subscribe((data) => console.log('new data logic');
    

    【讨论】:

    • 谢谢,由于更新不是在我的主APP代码上完成的,它是由firebase函数完成的。我的主 APP 需要监听来自 firestore 的更改并关闭加载。
    • 您可以使用valueChanges of FireStoreDocument 在文档更新时执行一些代码
    • 是的,我知道 valueChange。我可以在 html 中使用异步管道来更新更改。但是如果我想检测我的 TS 文件中的更改,我应该调用什么命令?从您在 this.db.doc('item/1).valueChanges() .subscribe((data) => this.name = data.name; 上面的回答中,我可以等待名称更改为“Eric”以解除loading? if(this.name == 'Eric'){ loading.dismiss()}
    • 您将任何您想要的逻辑放入订阅块中
    猜你喜欢
    • 2023-03-27
    • 2020-03-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多