I have an Ionic 2 project where I'm using Firebase and I've been trying to master the art of creating many-to-many relationships, utilizing observables. In some of my previous questions, I've been making some progress where I can aggregate separate paths into one observable.
However, using my latest code, it would seem that when I update the data in the Firebase database, it only updates the observable a single time. After that, the UI doesn't update and I have to refresh the page in order to pull the latest data from the Firebase database.
Here is my function:
public getUserProfile(data) {
return this._af.database
.object(`/social/users/${data.id}`)
// Switch to the joined observable
.switchMap((user) => {
let vidKeys = Object.keys(user.videos);
return Observable.combineLatest(
vidKeys.map((vidKey) => this._af.database
.object(`/social/videos/${vidKey}`)
),
(...videos) => {
vidKeys.forEach((vidKey, index) => { user.videos[vidKey] = videos[index] });
return user;
}
);
});
}
Update: Adding the code that calls this function.
In my component:
ionViewDidLoad() {
this._users.getUserProfile({id: 'c25zdGFyb3NjJJuYtREaYWlsLmNvbQ=='}).subscribe(success => {
this.user = success;
this.videoKeys = Object.keys(this.user.videos);
});
}
In my template:
<div *ngFor="let key of videoKeys">
{{user.videos[key].title}}
<img src="{{user.videos[key].thumbnail}}" />
</div>
Here is what my database object for my user looks like:
{
users: {
c25zdGFyb3NjJJuYtREaYWlsLmNvbQ==
videos {
AFA-rOls8YA: true,
AlLsp4gnyDQ: true,
dX_1B0w7Hzc: true,
ik5qR5bw75E: true,
njos57IJf-0: true
},
videos {
AFA-rOls8YA {
attached_members {
c25zdGFyb3NjJJuYtREaYWlsLmNvbQ==: true
}
title: "Hello...it's me"
},
AlLsp4gnyDQ: { ... },
dX_1B0w7Hzc: { ... },
}
}
}
}
This question is an extension of my previous question here.
I'm still relatively new to observables, but does anyone think they have any idea what could be going on here?
getUserProfile? Can you confirm that you don't usefirstortake(1)on the returned observable? - cartant