zafer
zafer

Reputation: 449

Angular Firebase Reachig Child array of an Item

I had an issue with reaching items under key. My firebase structure is like down below:

Firebase data

In code part I can reach the key value but I cant list the values under key such as '2' key.

My html part is like the code below:

<li *ngFor="let item of listRequest;let i=index">
        <tr>{{item.key}}</tr>
      <td><button >Approve</button> </td>
</li>

This is how I pull datas from firebase(service part):

return  this.db.list('/requestCourses/').snapshotChanges().pipe(map(changes => changes
           .map(c => ({key: c.payload.key, ...c.payload.val()}))));

Upvotes: 2

Views: 214

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

To get the uid, you can do the following:

First in the service class do the following:

getItems()
{
return this.db.list('/requestCourses/').snapshotChanges();
}

getSubItems(key)
{
return this.db.list('/requestCourses/' + key + "/").snapshotChanges();
}

Then in the component you can subsribe:

this.getItems().subscribe(items => {
    items.forEach(values =>{
    let key = values.key;
    getSubItems(key).subscribe(subItems => {
       subItems.forEach(uid => {
         console.log(uid.payload.val());
         });
       });
     });
   });

Here first add a reference to node requestCourses, then add snapshotChanges() which returns an Observable, and using subscribe you can retrieve the array that contains the key.

Then using the key you can add a reference to the node key, subscribe to the Observable and iterate in the array to get the uid.

Upvotes: 1

Related Questions