Reputation: 383
Here is the structure of data:
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": {
"profilePicture": "https://firebasestorage.googleapis.com/v0/b/items-222f4.appspot.com/o/itemPictures%2F-L1zPnV8eFpyTZlRZSkS%2FItemPicture.png?alt=media&token=974cf1c7-974b-4f10-89bd-9033ef25d476",
"guestName": "Test"
}
},
{
"id": "-L1zQ41CVIwKTbAA_yiQ",
"guestList": {
"profilePicture": "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",
"guestName": "Test"
}
},
{
"id": "-L1zQKQBrnpgVdwqIdsU",
"guestList": {
"profilePicture": "https://firebasestorage.googleapis.com/v0/b/items-222f4.appspot.com/o/itemPictures%2F-L1zQKQBrnpgVdwqIdsU%2FItemPicture.png?alt=media&token=ab7d193c-8938-4820-aa8d-d091c9d89211",
"guestName": "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
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