I have a 3 parallel firebase requires. And I need to join them as 1 single object. Code as below:
import {Http} from "@angular/http";
import {AngularFireDatabase} from "angularfire2";
import {Thread} from "../model/thread";
import {Message} from "../model/message";
import {Participant} from "../model/participant";
constructor(private http: Http, private db:AngularFireDatabase) { }
loadUserThreads(uid): Observable<AllUserData> {
return Observable.forkJoin([
this.db.list('participant/' + uid + '/threads').first(),
this.db.list('participant/' + uid + '/messages').first(),
this.db.list('participant/' + uid + '/participants').first()
])
.map((data:any[]) => {
let threads: Thread[] = data[0];
let messages: Message[] = data[1];
let participants: Participant[] = data[2];
let AllUserData = {
threads: threads,
messages: messages,
participants: participants
}
return AllUserData;
})
}
This work and return the exact format I need, which is with this interface:
export interface AllUserData {
participants: Participant[];
threads: Thread[];
messages: Message[];
}
The problem is forkJoin won't run without .first() all the firebase observables. And if I use .first() to complete all the firebase observables, I LOST the dynamic updates which why I use firebase the first place. How can I get the best of both world? - Keeping the firebase observables live but can join them like my interface format? I am pulling my hair out on this. Any help will be greatly appreciated!
Instead of .forkJoin(...)
you could use .combineLatest(...)
.
I also did streamline your map
a bit:
Observable.combineLatest(
this.db.list('participant/' + uid + '/threads'),
this.db.list('participant/' + uid + '/messages'),
this.db.list('participant/' + uid + '/participants')
)
.map(([threads, messages, participants]: [Thread[], Message[], Participant[]]) =>
({ threads, messages, participants })
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With