Why do I get a child_added event when I delete?

The following code creates an infinite loop of repeated child_added, although I never add a child. Nodes are deleted. 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 am registering, the same four nodes (A, B, C, D) are deleted:

 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
source share
2 answers

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

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

It is not obvious that the behavior will be different from the documentation, but startAt (null) does not produce the same behavior (although in this case it should). I would like this to be documented.

+1
source

I was able to reproduce the problem. I think this is because Query trying to keep items in its β€œwindow” when deleted.

You can work around this problem by using once('value' instead of on('child_added' , for example:

 notifications.startAt(null).once('value', processNotifications); function processNotifications(notifications) { notifications.forEach(function(notification) { notification.ref().remove(); }); } 

Since value only works once , it will not interfere with the remove operation.

+1
source

Source: https://habr.com/ru/post/1201284/


All Articles