I'm looking to display a list of posts similar to this: Post List
In order to indicate which post is favorited by a user, I need to retrieve data from two different collections in my MongoDB database. The ngOnInit function in my post-list.component.ts file currently appears as follows:
ngOnInit() {
this.isLoading = true;
this.postsService.getPosts(this.postsPerPage, this.currentPage);
this.favoritesService.getFavorites(this.postsPerPage, this.currentPage);
this.userId = this.authService.getUserId();
this.postsSub = this.postsService
.getPostUpdateListener()
.subscribe((postData: { posts: Post[]; postCount: number }) => {
this.totalPosts = postData.postCount;
this.posts = postData.posts;
console.log("Posts fetched successfully!");
});
this.favoritesSub = this.favoritesService
.getFavoriteUpdateListener()
.subscribe(
(favoriteData: { favorites: Favorite[]; postCount: number }) => {
this.isLoading = false;
this.favorites = favoriteData.favorites;
this.fetchFavorites();
console.log("Favorites fetched successfully!");
}
);
this.userIsAuthenticated = this.authService.getIsAuth();
this.authStatusSub = this.authService
.getAuthStatusListener()
.subscribe((isAuthenticated) => {
this.userIsAuthenticated = isAuthenticated;
this.userId = this.authService.getUserId();
});
}
Is there a way to await the arrival of the post data first?