我正在为足球运动员建立一个社交网站。我第一次尝试使用 AngularFire,但我似乎没有得到我想要的。我想在 getAllPosts()
致电 ngOnInit()
。
在函数中,我想获取登录用户关注的玩家列表,包括他自己(这样我就可以检索登录用户的帖子)。然后我想遍历它们并检索它们各自的帖子并按创建日期对帖子进行排序。
这是我目前想到的,但它并不像我期望的那样工作。首先,我不确定我是否应该使用 valueChanges()
或 snapshotChanges()
或其他任何东西。 console.log(players)
应该打印一次,但打印的数量与 userFollowing
集合中的玩家数量一样多,这让我感到困惑,而且 console.log(posts, 'postssss');
的打印量超出预期。>
getAllPosts() {
this.isLoading = true;
let players = [{ handle: this.user?.handle }];
this.db
.collection<string>(`following/${this.user?.handle}/userFollowing`)
.valueChanges({ idField: 'handle' })
.subscribe((data) => {
players.push(...data);
console.log(players);
players.forEach((player) => {
this.db
.collection<Post>(`posts/${player.handle}/userPosts`, (ref) =>
ref.orderBy('createdAt', 'desc')
)
.valueChanges({ idField: 'id' })
.subscribe((posts) => {
console.log(posts, 'postssss');
this.isLoading = false;
});
});
});
}
这不是完整的解决方案。我很困惑,所以我想我会征求建议。我想将结果存储在 posts?: Observable<Post[]>;
中并循环遍历它 <app-post *ngFor="let post of posts | async" [post]="post"></app-post>