Update:
The issue with encountering empty value fields was due to missing keys in the database, so much of the discussion here may not be relevant to your specific question. If you are seeking a way to merge queries in AngularFire2, the provided answer below does a solid job at accomplishing this. I am currently utilizing combineLatest
instead of forkJoin
. To implement this, make sure to include
import 'rxjs/add/observable/combineLatest';
.
My Firebase structure is denormalized as follows:
members
-memberid1
-threads
-threadid1: true,
-threadid3: true
-username: "Adam"
...
threads
-threadid1
-author: memberid3
-quality: 67
-participants
-memberid2: true,
-memberid3: true
...
I aim to display the username
in my threads
view, which is sorted by quality
.
Here is 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
}
});
}
This is how my component is set up:
ngOnInit() {
this.threads = this.featuredThreadsService.getFeaturedThreads()
this.threads.subscribe(
allThreads =>
allThreads.forEach(thisThread => {
thisThread.username = this.featuredThreadsService.getUsername(thisThread.author)
console.log(thisThread.username)
})
)
}
Despite having implemented this setup, unfulfilled observables seem to be logged to the console for some reason.
https://i.sstatic.net/W8XlO.png
I am looking to assign these values to a property of threads
so that it can be rendered in my view like this:
<div *ngFor="let thread of threads | async" class="thread-tile">
...
{{threads.username}}
...
</div>
Update: Included console.log
for allThreads
and thisThread
https://i.sstatic.net/tpXeU.png
https://i.sstatic.net/iO408.png
Update: Subscribed to getUsername()
this.featuredThreadsService.getUsername(thisThread.author)
.subscribe( username => console.log(username))
Resulting in objects without any values being displayed: