Firebase child_ added 仅适用于新项目 [英] Firebase child_added for new items only
问题描述
我在 Redux 中使用 Firebase 和 Node.我正在从一个键加载所有对象,如下所示.
I am using Firebase and Node with Redux. I am loading all objects from a key as follows.
firebaseDb.child('invites').on('child_added', snapshot => {
})
这种方法背后的想法是,我们从数据库中获取一个有效负载,并且只使用一个操作来通过 Reducers 更新我的本地数据存储.
The idea behind this method is that we get a payload from the database and only use one action to updated my local data stores via the Reducers.
接下来,我需要监听关键邀请的任何NEW 或UPDATED 子级.然而,现在的问题是 child_ added 事件会为所有现有的键以及新添加的键触发.我不想要这种行为,我只需要新密钥,因为我检索了现有数据.
Next, I need to listen for any NEW or UPDATED children of the key invite. The problem now, however, is that the child_added event triggers for all existing keys, as well as newly added ones. I do not want this behaviour, I only require new keys, as I have the existing data retrieved.
我知道 child_ added 通常用于这种类型的操作,但是,我希望减少触发的操作数量,并因此触发渲染.
I am aware that child_added is typically used for this type of operation, however, i wish to reduce the number of actions fired, and renders triggered as a result.
实现这一目标的最佳模式是什么?
What would be the best pattern to achieve this goal?
谢谢,
推荐答案
我已经用下面的方法解决了这个问题.
I have solved the problem using the following method.
firebaseDb.child('invites').limitToLast(1).on('child_added', cb)
firebaseDb.child('invites').on('child_changed', cb)
limitToLast(1) 获取邀请的最后一个子对象,然后侦听任何新的子对象,将快照对象传递给 cb 回调.
limitToLast(1) gets the last child object of invites, and then listens for any new ones, passing a snapshot object to the cb callback.
child_changed 侦听对邀请的任何子更新,将快照传递给 cb
child_changed listens for any child update to invites, passing a snapshot to the cb
这篇关于Firebase child_ added 仅适用于新项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!