下面的代码会产生一个无限循环的重复child_added,尽管我从不添加一个孩子。节点被删除。我只是永远地循环。我该如何解决这个问题?
//notifications branch is pre-populated when I get here:
notifications.startAt(null).on('child_added', processNotification, logError);
function processNotification(notification)
{
removeNotification(notification);
}
function removeNotification(notification)
{
var child = notifications.child(notification.name());
console.log("REMOVING", child.toString());
child.remove();
}
我记录的是相同的四个节点(A,B,C,D)被删除:
REMOVING https://my.firebaseio.com/experimental/-JVMi0_4poXfOWUt5g49 - A
REMOVING https://my.firebaseio.com/experimental/-JVMi1Y_bFwZAkffRel4 - B
REMOVING https://my.firebaseio.com/experimental/-JVMi2lyhKj8z27ik71x - C
REMOVING https://my.firebaseio.com/experimental/-JVMhzazdgHYAstqxu8L - D
REMOVING https://my.firebaseio.com/experimental/-JVMi0_4poXfOWUt5g49 - A
REMOVING https://my.firebaseio.com/experimental/-JVMi1Y_bFwZAkffRel4 - B
REMOVING https://my.firebaseio.com/experimental/-JVMi2lyhKj8z27ik71x - C
REMOVING https://mu.firebaseio.com/experimental/-JVMhzazdgHYAstqxu8L - D
...
答案 0 :(得分:1)
我能够重现这个问题。我认为这是因为Query
在删除项目时会尝试将项目保留在“窗口”中。
您可以使用once('value'
代替on('child_added'
解决此问题,如下所示:
notifications.startAt(null).once('value', processNotifications);
function processNotifications(notifications)
{
notifications.forEach(function(notification) {
notification.ref().remove();
});
}
由于value
仅触发once
,因此不会干扰remove
操作。
答案 1 :(得分:1)
最终,我刚刚删除了查询并进行了正常处理,删除了“startAt(null)”:
notifications.on('child_added', processNotification, logError);
行为与文档不同并不明显,但startAt(null)不会产生相同的行为(尽管它应该是这样)。我希望看到这个更好的记录。