Update:
The issues I was encountering with the empty value fields had to do with non-existent keys in my database, so most of the discourse here won't apply to your question. If you're looking for a way to 'join' queries in AngularFire2, the accepted answer below does a fine job of this. I'm currently using combineLatest
instead of forkJoin
. In order to do this you have to import 'rxjs/add/observable/combineLatest';
.
I have the following denormalized Firebase structure:
members
-memberid1
-threads
-threadid1: true,
-threadid3: true
-username: "Adam"
...
threads
-threadid1
-author: memberid3
-quality: 67
-participants
-memberid2: true,
-memberid3: true
...
I want to render username
in my threads
view, which is sorted by quality
.
My service:
getUsername(memberKey: string) {
return this.af.database.object('/members/' + memberKey + '/username')
}
getFeaturedThreads(): FirebaseListObservable<any[]> {
return this.af.database.list('/threads', {
query: {
orderByChild: 'quality',
endAt: 10
}
});
}
My component:
ngOnInit() {
this.threads = this.featuredThreadsService.getFeaturedThreads()
this.threads.subscribe(
allThreads =>
allThreads.forEach(thisThread => {
thisThread.username = this.featuredThreadsService.getUsername(thisThread.author)
console.log(thisThread.username)
})
)
}
For some reason this logs what looks like unfulfilled observables to the console.
I'd like to get these values into a property of threads
so I can render it in my view like this:
<div *ngFor="let thread of threads | async" class="thread-tile">
...
{{threads.username}}
...
</div>
Updated: console.log
for allThreads
and thisThread
Updated: subscribed to getUsername()
this.featuredThreadsService.getUsername(thisThread.author)
.subscribe( username => console.log(username))
The result of this is objects with no values: