I have a collection of artists. Some of those artist documents have an albums collection underneath which will have individual album documents. I'm trying to create a view in my html that displays each artist and their albums. However, I'm having a little difficulty building the array sufficiently. This is as far as I have got:
arrayForTemplate = [];
--
fetchArtists(){
var albums_array = [];
this.db.collection(`pathexample`).orderBy("order").get().then((querySnapshot) => {
querySnapshot.forEach((artist) => {
const artist_data = artist.data();
const artist_id = artist.id;
artist_data["id"] = artist_id;
// Push the list of artists into the array
this.arrayForTemplate.push(artist_data)
this.db.collection(`pathexample/${artist_id}/albums`).orderBy("order").get().then((querySnapshot) => {
querySnapshot.forEach((album) => {
const album_data = album.data();
const album_id = album.id;
// Push an array of albums into each artist
var album_information = {};
album_information["album_photo"] = album_data.album_photo;
album_information["album_date"] = album_data.album_date;
albums_array.push(album_information)
});
});
});
})
.catch((error) => {
console.log("Error getting documents: ", error);
});
}
- Array in my template
- Perform a query to get all artists
- Push the returned data into the array
- For each of the artists, query the albums collection based on the artist ID
- For each album, create an object with the data I wish to include in the array
- Update the
albums_arraywith the new data
The question I now have is: How do I push all of this data into the arrayForTemplate array so that the data is structured like this: (Please let me know if this is bad practise or if there's a better away to achieve this)
- Artist 1
- Artist 2
- [albums]
- album photo
- album date
- [albums]
- Artist 3
- [albums]
- album photo
- album date
- [albums]
The goal is to have the HTML as follows:
<div *ngFor="let artist of arrayForTemplate">
<div> {{ artist.name }} </div>
<div *ngFor="let album of artist.albums"> {{ album.album.photo }} </div>
</div>
artist_name. I've just renamed the default firebase sdk const values, which would original beconst data = doc.data()for example. Does that help?