So I've been trying to use a forkJoin
with an array of Observable from firebase, but it seems they don't complete.
I've tried to apply .first()
or .take(1)
since it is supposed to complete the observable but it doesn't seem to change anything.
getChat(userTwoId: string)
{
return this.auth.getAuthenticatedUser()
.map(auth =>
{
console.log(auth.uid);
return auth.uid;
})
.mergeMap(uid =>
{
console.log(`/user-messages/${uid}/${userTwoId}/`);
return this.database.list(`/user-messages/${uid}/${userTwoId}/`).snapshotChanges();
})
.mergeMap(chats =>
{
const oChats = chats.map(chat=>
{
console.log(`/messages/${chat.key}`);
return this.database.object(`/messages/${chat.key}`).valueChanges().first();
});
console.log('ObsevableMap');
console.log(oChats);
return Observable.forkJoin(oChats);
});
}
My Observables (from the array) return something when I subscribe to them individually but the forkJoin
doesn't.
So I've seen somewhere that Firebase Observable never complete. If that is true what would be the best alternative. And if it isn't what would complete them? isn't there a way to force them to complete?
more detail: So so here is the code that I'm trying to test:
this.auth.getAuthenticatedUser()
.map(auth =>
{
return auth.uid;
})
.mergeMap(uid =>
{
console.log('path to list of keys:')
console.log(`/user-messages/${uid}/${userTwoId}/`);
return this.database.list(`/user-messages/${uid}/${userTwoId}/`).snapshotChanges();
})
.mergeMap(chats =>
{
console.log('Log each path to object:');
const oChats = chats.map(chat=>
{
console.log(`/messages/${chat.key}`);
return this.database.object(`/messages/${chat.key}`).valueChanges().take(1);
});
//See if its log
//console.log('Log each Obs subscription:')
//oChats.forEach(a=>{a.subscribe(b=>{console.log(b)})});
return Observable.forkJoin(oChats);
}).subscribe(vals=>
{
console.log('Fork Subscription:');
console.log(vals)
});
this.auth.getAuthenticatedUser()
returns an observable of the current Firebase User then I map it to get the uid.
In my database there is a node which begins with the user uid then a second user uid and then a list of key of messages they've sent each other.
So I return these key to finnaly map each key to an observable that returns their message.
After there is a commented subscribtion to test they return something.
And then I try to fork or merge them but when I subscribe nothing happens.
I've tried with first()
instead of take(1)
.
I've tried my best to be clear but I'm sorry not to be^^
and here is my log when the subscription is uncommented
More Update
Well I tried to make a demo on stackBlitz here
https://stackblitz.com/edit/angular-template-firebase-yvbkho
I copied and pasted most of my code and made another firebase database with the same structure here:
And my demo works.