Angular cannot use async pipe with my observables

Robert Stone

I want to use async pipe in my template , but I have a problem getting data from my api. I have used behavior subject to next the data and then keep it in an observable. When I call the observable in html I get this 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>

enter image description here

I really want to understand why it doesn't work pls

Max

This will help you if you need to use a BehaviorSubject with an asynchronous pipeline. https://angular.io/api/common/AsyncPipe

Your component:

friend$: Observable<[]> = [];

getFriendList(userId: string) {
  this.friend$ = this.mainService.getLastFriend(+userId).pipe(
    catchError(error => of(error))
  );
}

your mainService:

private friendSubject = new BehaviorSubject<[]>(null);
private users = [
    {
        userId: 1,
        name: 'Max',
        friendList: {
          {id: 1, name: 'Bob'}, 
          {id: 2, name: 'Alex'},
          {id: 3, name: 'Kate'}
        }
    }
];

getLastFriend(userId: number): Observable<[]> {
  const friendList = this.getFriendList(userId);
  this.friendSubject.next(friendList);

  return this.friendSubject.asObservable();
}  

private getFriendList(userId: number): [] {
  return of(users).pipe(
    filter( ({userId}) => userId === userId),
    map(user => user.friendList)
  );
} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to handle boolean observables with the async pipe in Angular

From Dev

Angular async pipe and object property

From Dev

spyOn not working with async pipe in Angular

From Dev

Cannot find a differ supporting object ,Async pipe and ngFor problems in angular2

From Dev

Cannot display date pipe on Angular

From Dev

Cannot use awk pipe output

From Dev

Error handling with Angular2 async pipe

From Dev

The pipe 'async' could not be found - Angular 11

From Dev

The pipe 'async' could not be found - Angular 11

From Dev

Angular2 async pipe with observable

From Dev

Angular 2 using ngFor with the async pipe and index

From Dev

Extending Angular 2 ngModel directive to use observables

From Dev

Angular2 is this the right way to use Observables?

From Dev

Is my use of WCF async beneficial?

From Dev

Angular2 use basic pipe in custom pipe

From Dev

Angular 2 async pipe not rendering/updating Observable data automatically

From Java

Using an array from Observable Object with ngFor and Async Pipe Angular 2

From Dev

Safe navigation operator on an async observable and pipe in Angular 2

From Dev

Angular2 Observables - Creating my own - subscriber undefined

From Dev

How to use a pipe in a component in Angular 2?

From Dev

How to use a pipe in two different Angular Modules

From Dev

how to use pipe in angular 2 js?

From Dev

How to use the angular decimal pipe in typscript

From Dev

How can tornado use pipe to async send data to client?

From Dev

Is it really better to use async-pipe instead of subscribe() in this particular situation?

From Dev

How can tornado use pipe to async send data to client?

From Dev

Observables and how to use them correctly in Angular2

From Dev

angular2: How to use observables to debounce window:resize

From Dev

HTTP: Angular 2+TS How to use Observables in HTTP

Related Related

  1. 1

    How to handle boolean observables with the async pipe in Angular

  2. 2

    Angular async pipe and object property

  3. 3

    spyOn not working with async pipe in Angular

  4. 4

    Cannot find a differ supporting object ,Async pipe and ngFor problems in angular2

  5. 5

    Cannot display date pipe on Angular

  6. 6

    Cannot use awk pipe output

  7. 7

    Error handling with Angular2 async pipe

  8. 8

    The pipe 'async' could not be found - Angular 11

  9. 9

    The pipe 'async' could not be found - Angular 11

  10. 10

    Angular2 async pipe with observable

  11. 11

    Angular 2 using ngFor with the async pipe and index

  12. 12

    Extending Angular 2 ngModel directive to use observables

  13. 13

    Angular2 is this the right way to use Observables?

  14. 14

    Is my use of WCF async beneficial?

  15. 15

    Angular2 use basic pipe in custom pipe

  16. 16

    Angular 2 async pipe not rendering/updating Observable data automatically

  17. 17

    Using an array from Observable Object with ngFor and Async Pipe Angular 2

  18. 18

    Safe navigation operator on an async observable and pipe in Angular 2

  19. 19

    Angular2 Observables - Creating my own - subscriber undefined

  20. 20

    How to use a pipe in a component in Angular 2?

  21. 21

    How to use a pipe in two different Angular Modules

  22. 22

    how to use pipe in angular 2 js?

  23. 23

    How to use the angular decimal pipe in typscript

  24. 24

    How can tornado use pipe to async send data to client?

  25. 25

    Is it really better to use async-pipe instead of subscribe() in this particular situation?

  26. 26

    How can tornado use pipe to async send data to client?

  27. 27

    Observables and how to use them correctly in Angular2

  28. 28

    angular2: How to use observables to debounce window:resize

  29. 29

    HTTP: Angular 2+TS How to use Observables in HTTP

HotTag

Archive