I am trying to display nested data from Firebase to Ionic project. I have the following data structure in firebase:
My query:
let dbRefLists = firebase.database().ref('lists/');
var self = this;
dbRefLists.on('value', (querySnapshot) => {
this.lists = Object.keys(querySnapshot.val()).map(key => querySnapshot.val()[key]);
querySnapshot.forEach((data) => {
var listItem = data.val().id;
dbRefLists.child(`${listItem}/listItems`).on('value', (snapshotVal) => {
if (snapshotVal.val() !== null) {
self.lists.listItems = Object.keys(snapshotVal.val()).map(key => snapshotVal.val()[key]);
}
})
return self.lists.listItems;
})
return lists;
});
My HTML:
<ul>
<li *ngFor="let list of lists; let i=index" text-wrap (click)="toggleGroup(i)" [ngClass]="{active: isGroupShown(i)}">
<h2>
<span>{{list.title}}</span>
</h2>
<ul *ngIf="isGroupShown(i)">
<li *ngFor="let listItem of lists.listItems">
<span>{{listItem.title}}</span>
</li>
</ul>
</li>
</ul>
The result is that I receive list items in all the lists (List1 and List2), instead of only in List1:
Where is my mistake? Thanks