In the html of the component I have the following:
<ion-card *ngFor="let recipe of recipeListRef$ | async">
....
<button *ngIf="isExistingFavorite(recipe)">
....
</ion-card>
that method searches through the favorites array:
isExistingFavorite(recipe: Recipe) {
for(let f of this.favorites){
if(f.title === recipe.title){
return true;
}
}
return false;
}
the favorites array is loaded in the constructor:
favorites: Recipe[];
constructor() {
this.favoritesService.getFavorites()
.subscribe(
favorites => {
this.favorites = favorites;
}
);
}
the issue is that the page loads before the 'favorites' array is populated, and this results in the page crashing due to the array being null.
I am looking for a way to populate the array first, and then load the html.
~UPDATE~ I have added the following in the component but in the case that the user is authenticated, it always rejects although none of the console.logs print out at all...
ionViewCanEnter() {
return new Promise((resolve, reject) => {
if(this.authService.isAuthenticated()) {
this.favoritesService.getFavorites()
.subscribe(
favorites => {
console.log('got the data', favorites);
this.favorites = favorites;
resolve(favorites);
},
error => {
console.log('failed to get data', error);
reject(error);
}
);
}else{
resolve(true);
}
});
here is the getFavorites() service component:
favoriteListRef$: FirebaseListObservable<Recipe[]>;
constructor(private database: AngularFireDatabase) {
this.favoriteListRef$ = this.database.list('myUrl', { preserveSnapshot: true});
}
}
getFavorites() {
return this.favoriteListRef$;
}
favorites: Recipe[]=[];