Child_added starts after deletion

I created this handler to keep track of the creation of new children. This little trick stops the child_added event from running my code on every page load. Instead, it only fires up when something is actually added.

  // Listen for new rooms being created
  var first_room_added_after_load = true;
  this._roomRef.endAt().limit(1).on('child_added', function(snapshot){
      if (first_room_added_after_load) { // we want to skip the first room, because it will already exist
          first_room_added_after_load = false;
          return;
      }
      else {
          this._onCreateRoom(snapshot);
      }
  }, this);

      

However, when I call remove () on a node in _roomRef, then child_added is triggered for a node that already exists. Any clues on why and how to stop it?

I should also mention that this only happens if I delete what was created after something else in the list. If you delete the old elements first, then child_added does not start.

+3


source to share


1 answer


You seem to think it is endAt()

using a timestamp for comparison so that it only returns new children. But that's just not how it works. From Firebase Documentation forendAt

:

If no arguments are specified, the end point is the end of the data.

With code:

this._roomRef.endAt().limit(1)

      

You are creating a query that should always return one child. The condition is that the child was created after the request was created.

The request is like a sliding window over child nodes in your Firebase ref: it will always contain (at most) one child that was created after your request was created.

Example

Let's say you have these children when you create your request:

child1
child2
child3 <!-- last child present when creating the endAt() query -->

      

With the help the endAt

request will be "rooted" after child3

. Thus, it will not fire any events for child1

, child2

and child3

.

Let's say you add a new child:

child1
child2
child3 <!-- last child present when creating the endAt() query -->
child4

      



The request will be child_added

for child4

.

Now if we add another child:

child1
child2
child3 <!-- last child present when creating the endAt() query -->
child4
child5

      

The request will fire child_removed

for child4

and child_added

for child5

. Note that child4

it didn't actually get removed from your Firebase, it just disappeared from the query results (since you asked to limit the number of results to just one).

When we remove the last child, we end up with:

child1
child2
child3 <!-- last child present when creating the endAt() query -->
child4

      



And the query will fire child_removed

for child5

and child_added

for child4

. This is again due to the fact that your request always contains (at most) one node that was added after child3

.

How to get what you want

It looks like you only want to get child_added

one time for new added children. In this case, I would not use it limit

in the request, but instead I would set a timestamp that the room was created as its priority:

this._roomRef.push({
    /* whatever properties you need for a room */, 
    '.priority': Firebase.ServerValue.TIMESTAMP 
})

      

Then you can simply run the request with that priority.

this._roomRef.startAt(Date.now()).on('child_added', ...

      

+2


source







All Articles