为什么我要删除child_added事件? [英] Why do I get child_added event on remove?

查看:123
本文介绍了为什么我要删除child_added事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码产生了一个重复的child_added的无限循环,尽管我从来没有添加一个孩子。节点被删除。我只是永远循环。

  //通知分支是预先填充的,当我到达这里时:
notifications.startAt (null).on('child_added',processNotification,logError);

函数processNotification(通知)
{
removeNotification(notification);


函数removeNotification(通知)
{
var child = notifications.child(notification.name());
console.log(REMOVING,child.toString());
child.remove();

$ / code>

我记录的是相同的四个节点(A,B,C,D )被删除:

 删除https://my.firebaseio.com/experimental/-JVMi0_4poXfOWUt5g49  -  A 
REMOVING https://my.firebaseio.com/experimental/-JVMi1Y_bFwZAkffRel4 - B
删除https://my.firebaseio.com/experimental/-JVMi2lyhKj8z27ik71x - C
删除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
...


解决方案

最终,我放弃了查询并使用正常处理,删除startAt(null):

  notifications.on('child_added',processNotific ation,logError); 

行为与文档不同,但startAt(null)不行产生相同的行为(虽然在它应该)。我很乐意看到这个更好的记录。

The following code produces an endless loop of repeated child_added, though I never add a child. The nodes are removed. I just loop forever. How can I fix this?

//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();     
}

What I log is the same four nodes (A,B,C,D) being removed:

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
...

解决方案

Ultimately, I just dropped the query and went with normal processing, dropping the "startAt(null)":

notifications.on('child_added', processNotification, logError);

It's not obvious that the behavior will be different from the documentation, but the startAt(null) does not produce the same behavior (though in thoery it should). I would love to see this better documented.

这篇关于为什么我要删除child_added事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆