I've been trying to implement the async pipe in my template, but I'm encountering difficulties retrieving data from my API. To store and retrieve the data, I have utilized a behavior subject to create an observable. However, when I attempt to display the data on the HTML page, I encounter the following error:
Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to
Iterables such as Arrays
friendList.ts
public friendSubject = new BehaviorSubject<any>(null);
public friend$: Observable<any> = this.friendSubject.asObservable();
getFriendList(userID: string) {
userID = this.userID;
try {
return this.mainService.getFriendList(userID).subscribe((friendList) => {
console.log(friendList)
this.friendSubject.next(friendList);
// friendList.data.forEach((friend => {
// }))
console.log(this.friendSubject)
})
}
catch (err) {
}
}
friendList.html
<ion-label class="friend-count" *ngIf="friend$ | async">{{(friend$ | async)?.length}}</ion-label>
//this works..
<div class="friend-list">
<div *ngFor="let i = index; let friend of friend$ | async" class="friend">
<div class="custom-img">
<ion-img class="friend-img" [src]="friend?.frendsView.photo"></ion-img>
</div>
<ion-label></ion-label>
</div>
</div>
https://i.sstatic.net/PZpMF.png
I'm really eager to understand why this isn't working, so if anyone has any insights, please share. Thank you.