【问题标题】:Is it possible to get only the updated value with angularfire?是否可以使用 angularfire 仅获取更新的值?
【发布时间】:2020-11-12 03:48:13
【问题描述】:

我正在使用 angularfire 和实时数据库 我有一个数据库:

actions:{
  uid1: {
    meta...
  },
  uid2: {
    meta...
  },
  uid3: {
    meta...
  },
  uid4: {
    meta...
  },
}

我制作了一个显示所有这些操作的编辑器。现在假设我添加了一个操作 (uid5),当我只需要更新的值时,我的 valueChanges 方法将所有 uid 发回给我。

我会做一些这样的事情

ngOnInit(){
  this.db.object(`actions`).valueChanges().subscribe(
    (actions) => console.log(actions) // here I get first time uid1{}, uid2{} ..., uid4{}
    //second time after adding uid5 I would get uid5{} only.
  )
}

那么有可能吗,是否有一些特定的事件或其他什么,或者我应该提出功能请求?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database angularfire


    【解决方案1】:

    Firebase 或 AngularFire 没有内置任何东西来仅获取新项目,因此您必须自己构建一些东西。

    两个最常见的选项:

    1. 在每个节点中存储一个timestamp 值,然后使用ref.orderBy("timestamp").startAt(Date.now()) 之类的内容仅查询“现在”之后的项目。
    2. 现在从键开始,例如 ref.orderByKey().startAt(ref.push().key)

    【讨论】:

    • 我在看那个,但是 'child_changed', "child_removed"... 怎么样。你的技术可以工作,但只适用于未更改和更新的添加元素。提前致谢。
    • 这实际上取决于您存储的时间戳。例如,如果您存储节点上次修改的时间戳,您可以查询项目的修改时间。
    • 是的,但 child_added/removed/changed 我已经实现了其中的一些,看起来它正在工作。会有什么区别? ...
    • 收听child_ 事件对于从服务器检索到的数据没有影响。因此,如果您只收听child_changed,那么您的应用仍在检索actions 下的所有节点。
    • 删除时:在这种情况下,您必须在数据库中存储已删除某些内容的事实。
    猜你喜欢
    • 2014-01-21
    • 1970-01-01
    • 2013-12-04
    • 2016-09-13
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多