Reputation: 1115
quick question about Firebase lists.
I'm trying to access multiple lists to iterate through that are in a single object. The object looks like this:
user : {
playlists: {}, // List 1
joined: {}, // List 2
favorites: {} // List 3
}
Now I know I can access all three lists by doing three separate requests like:
firebase.list('user/playlists');
firebase.list('user/joined');
firebase.list('user/favorites');
But I'm trying to clean up my code and do it all in one by using:
firebase.list('user');
Then accessing all the lists it returns inside of the user. The only problem is, the lists aren't returned with keys, it just says "object" instead. When I use firebase.object('user')
the keys are available.
This is what it looks like when I request 'user' as a list:
As you can see the key is inside the object and not outside of it ($key). How do I go about accessing these objects like I would if the key was actually the key (eg. user.playlists).
Upvotes: 1
Views: 1032
Reputation: 1115
For anyone interested I figured it out. I used the .map()
to convert parts of the object into arrays.
getPlaylist(_playlistId: string, _query: Object = {}) {
return this._database.object(`playlists/${_playlistId}`, _query).map(
result => {
return {
info: result.info,
leader: result.leader,
favorites: (result.favorites) ? Object.values(result.favorites) : [],
followers: (result.followers) ? Object.values(result.followers) : [],
songs: (result.songs) ? Object.values(result.songs) : []
};
}
);
}
For example, favorites is an object that I want to be returned as a list:
Check if exists If exists, map obj to array else assign to empty array
(result.favorites) ? Object.values(result.favorites) : []
Upvotes: 1