For a social media app, I have a collection of feed objects referenced by their IDs using AngularFire 2. Once each of these IDs has its relevant data pulled from the database that stores the actual feed objects, I wish to update the feedCards Observable object with this information so I can asynchronously display a collection of feed objects in my HTML. It's a pretty confusing chain of events, so let me summarize it for you.
Step-by-step Approach
displayFeed()is invoked right before theNavControllerloads thefeedcomponent on theMainpage.displayFeed()gets thetwineritem, which is essentially a user profile item, and then stores the user profile in theuserProfilevariable.- Once the user profile is loaded, the global
feedCardsObservable is set equal toloadFeed(), which returns an Observable array. loadFeed()uses theidvalue of theuserProfileglobal object to load the list of feed references stored in the user profile.- This snapshot is then subscribed to and the local
feedvariable is set equal to the result list of feed references. loadFeedreturns an Observable object in which thefeedreference list is mapped by the data each feed reference contains.pullFeedData(number)takes in a reference to a feed object and returns an observable with the associated feed data.
What Works
alert(JSON.stringify(feedData));alerts the correctfeedobjectBasically everything else.
What Doesn't Work
feed.map(...does not update the feed array becausepullFeedData(number)pulls and returns thefeedDataasynchronously. Thus,alert(JSON.stringify(data));indisplayFeed()alerts[null].
Code
feed.ts
userProfile:any;
feed: FirebaseListObservable<any>;
feedData: FirebaseObjectObservable<any>;
feedCards: Observable<any[]>;
constructor(public db: AngularFireDatabase, public nativeStorage: NativeStorage) {}
displayFeed():void {
this.nativeStorage.getItem('twiner').then((res) => {
this.userProfile = res;
this.feedCards = this.loadFeed();
this.feedCards.subscribe((data)=>{
alert(JSON.stringify(data));
})
});
}
loadFeed():Observable<any[]> {
var feed;
this.feed = this.db.list('/twiners/' + this.userProfile.id + '/feed', { preserveSnapshot: true });
this.feed.subscribe((snapshots)=>{feed = snapshots});
return Observable.of(feed.map((snapshot) => {
this.pullFeedData(snapshot.val()).subscribe((feedData)=>{
alert(JSON.stringify(feedData));
return feedData;
});
})).delay(1000);
}
pullFeedData(twine:number):any {
return this.db.object('/twines/' + twine, { preserveSnapshot: true });
}
feed.html
<ion-content fullscreen scroll="true" overflow-scroll="true">
<ion-card *ngIf="feedCards | async">feedCards exist</ion-card>
<twine-feed-card *ngFor="let feedCard of feedCards | async"
[data]="feedCard"
></twine-feed-card>
</ion-content>
Summary
feed.map does not update feed with feed objects because the new data is being pulled asynchronously. I need a fix for this.
Thank you!