How to get key from a Firebase list through Angularfire 5.0

2k views Asked by At

I was trying to upgrade my Angularfire 4 code to its latest version. There are some breaking changes as it no longer emits $key. The official documentation states to manage it by nesting map operators that I am struggling hard to understand -

constructor(afDb: AngularFireDatabase) {
  afDb.list('items').snapshotChanges().map(actions => {
    return actions.map(action => ({ key: action.key, ...action.payload.val() }));
  }).subscribe(items => {
    return items.map(item => item.key);
  });
}

How is this nested map working? Why can't I perform the same operation in single map operator?

1

There are 1 answers

1
RobrechtVM On

You have to map the list to a list containing this key value. I just played around a bit with it and this is my firebase 4 vs firebase 5 code

Old code :

.map(gamesList => {
   console.log(gamesList.$key)
})

New code :

.map(gamesList => 
{ 
     return gamesList.map(action => ({ key: action.key, ...action.payload.val() 
})
.map(gamesKey => {
    console.log(gamesKey.key)
})