I have a function that should return an observable with a list of Conversations.
To get that, I have to fetch the user conversations ids from another table Participants and combine the those fetch one by one.
public getConversations(user_id: string): Observable<Conversation[]> {
return this.firebase
.collection<Participant>(this.PARTICIPANTS_COLLECTION, ref =>
ref.where('user_id', '==', user_id)
)
.valueChanges()
.map((participants: Participant[]) => {
const conversation_ids: string[] = [];
participants.forEach((p: Participant) => conversation_ids.push(p.conversation_id));
return conversation_ids;
})
.mergeMap((ids: string[]) => {
let conversations: Observable<Conversation[]> = new Observable<Conversation[]>();
for(let id of ids) {
conversations.merge(
this.firebase.collection<Conversation>(this.CONVERSATIONS_COLLECTION, ref =>
ref.where('id', '==', id)
).valueChanges() // I want to merge these
);
}
return conversations; // I want to return this
});
}
Which operator should I use to combine those observables?