Al007
Al007

Reputation: 383

Retrieve a child that is inside another child to an array in Firebase

Here is the structure of data: enter image description here

I am trying to retrieve "guestName" and "profilePicture" of the guestList to an array (this.guestList). I am getting a snapshot using this code:

this.itemProvider.getguestList(this.navParams.get('itemId'))
        .on('value', guestSnapshot => {
        
          guestSnapshot.forEach(snap => {
            this.guestList.push({
              id:           snap.key,
              guest:        snap.val()         
            });
            
            return false;
          });

I am getting the array like this:

console.log:

eventList​ length​ ​3​   [
  {
    "id": "-L1zPnV8eFpyTZlRZSkS",
    "guestList": {
      "profile​Picture": "https://firebasestorage.googleapis.com/v0/b/items-222f4.appspot.com/o/itemPictures%2F-L1zPnV8eFpyTZlRZSkS%2FItemPicture.png?alt=media&token=974cf1c7-974b-4f10-89bd-9033ef25d476",
      "​guest​Name": "Test"
    }
  },
  {
    "id": "-L1zQ41CVIwKTbAA_yiQ",
    "​guestList": {
      "​profile​Picture": "https://firebasestorage.googleapis.com/v0/b/items-222f4.appspot.com/o/itemPictures%2F-L1zQ41CVIwKTbAA_yiQ%2FItemPicture.png?alt=media&token=bd32f971-7b81-4891-8f1f-9fd47185d105",
      "​guest​Name": "Test"
    }
  },
  {
    "id": "-L1zQKQBrnpgVdwqIdsU",
    "guestList": {
      "profile​Picture": "https://firebasestorage.googleapis.com/v0/b/items-222f4.appspot.com/o/itemPictures%2F-L1zQKQBrnpgVdwqIdsU%2FItemPicture.png?alt=media&token=ab7d193c-8938-4820-aa8d-d091c9d89211",
      "​guest​Name": "Test"
    }
  }
]

I need to rich "profilePicture" and guestName to be the members of the array. What do I need to change?

Upvotes: 0

Views: 56

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

You can either sub-address the snapshot or sub-address the value.

Sub-addressing in the snapshot is done with child():

      guestSnapshot.forEach(snap => {
        this.guestList.push({
          id:           snap.key,
          guest:        snap.child('"guestlist").val()         
        });

Sub-addressing in the value is done with:

      guestSnapshot.forEach(snap => {
        this.guestList.push({
          id:           snap.key,
          guest:        snap.val().guestlist
        });

Upvotes: 1

Related Questions