Azhar Ch
Azhar Ch

Reputation: 1

How to get a key of node in angularfirelist or angularfireobject in firebase using Ionic 3?

I'm trying to get my firebase data node keys using AngularFireList

Data.ts

getAddress(user){
this.AngularFireDatabase.list(`Profiles/Users/${user.uid}/address`);}



SendingData.ts



this.data.getAddress(user).valueChanges().subscribe(item=>{})

I'm getting everything that I have in child nodes, how can I get the key ? for example in the image I have Address/Home/ and in-home I have a name , lat, lng. here I want to get Home which is the key of these child nodes.

Firebase Data Image:

enter image description here

Any Solution?

Upvotes: 0

Views: 1388

Answers (1)

Suliman Twfiq
Suliman Twfiq

Reputation: 41

If you want to get the key you must use snapshotChanges() instead of valueChanges() , read Angularfire2 docs.


This is one of my codes look at it , in the TS file :

ParkingSpotList$: Observable<any[]>; // write this as global variable before constructor 
 this.ParkingSpotList$ = this.AFD.ShowParkingSpot().snapshotChanges().map(changes => {
  console.log(changes)
  return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }))
});

then in HTML (i get the key by writing "PS.key") :

  <ion-list>
    <ion-item  *ngFor="let PS of ParkingSpotList$ | async" (click)="ReserveParkingSpot(PS.key)">
      {{PS.PlateNo}}
    </ion-item>
  </ion-list>

Upvotes: 4

Related Questions