angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Can not implement child_removed event with angularfire2

Open johnrivelt opened this issue 8 years ago • 2 comments

I created a question on stack overflow and no one answered but someone made a comment to post here the issue. stack overflow

Firebase data structure

{
  markers: 
      key1 : {lat:1, lng:2}
      key2 : {lat:3, lng:4}           
}

with JS + firebase, all 3 events works well.

var markers = database.ref('markers');
markers.on('child_added',  function(snapshot) {
    addMarkerUI(snapshot);
});  
markers.on('child_changed', function(snapshot){
    updateMarkerUI(snapshot);
});
markers.on('child_removed', function(snapshot, prevChildKey) {
    removeMarkerUI(snapshot); // when delete child from server, this event is triggered
});  

But with angularfire2, it behaved very differently.

    this.items$ = this.db.list('markers');
    this.items$.snapshotChanges(['child_added', 'child_changed', 'child_removed'])
      .subscribe(actions => {
        console.log(actions);
        actions.forEach(action => {
          if (action.type === 'child_added') {// works
            console.log('I was added', action, action.payload.val())
          }

          if (action.type === 'child_changed') {// works
            console.log('I was modified', action, action.payload.val())
          }

          if (action.type === 'child_removed') {// does not works
            console.log('I was deleted', action, action.payload.val())
          }
        });
      });

Version info

https://stackblitz.com/edit/angular-um7hbv

How to reproduce these conditions

Failing test unit, Plunkr, or JSFiddle demonstrating the problem

https://stackblitz.com/edit/angular-um7hbv

Debug output

go to https://stackblitz.com/edit/angular-um7hbv and see console please.

Expected behavior

child_removed event does not occur in angularfire2 snapshotChanges

Actual behavior

"child_removed" event just returns actions without deleted child

johnrivelt avatar Mar 15 '18 07:03 johnrivelt

@johnrivelt snapshotChanges() is the state of your list, therefore you will never receive a child removed event because the item is gone from the state. If you want events use stateChanges(['child_removed').

davideast avatar Mar 18 '18 03:03 davideast

Great, thank you.

On Sun, Mar 18, 2018 at 2:49 PM, David East [email protected] wrote:

snapshotChanges() is the state of your list, therefore you will never receive a child removed event because the item is gone from the state. If you want events use stateChanges(['child_removed').

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/angular/angularfire2/issues/1513#issuecomment-373970887, or mute the thread https://github.com/notifications/unsubscribe-auth/AAuzKGsbYBNG8dM0ao707e5N8fVV3g4Fks5tfdlCgaJpZM4SrrNW .

-- Regards John, Cho 王言, 趙 Wang Eon, Cho

johnrivelt avatar Mar 18 '18 05:03 johnrivelt