Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 9 Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays

I am using async pipe in my template for an Observable:

applicants$: Observable<UserProfile[]>;
  ngOnInit() {
    this.applicants$ = this.store.pipe(
      select(fromRootUserProfileState.getUserProfiles)
    ) as Observable<UserProfile[]>;
}

Here is UserProfile.ts interface:

import { Role } from './role/role';
import { Country } from './Country';

export interface UserProfile {
  id?: number;
  fullName?: string;
  roles?: Role[];
  windowsAccount?: string;
  firstName?: string;
  lastName?: string;
  email?: string;
  managerName?: string;
  managerId?: number;
  managerEmail?: string;
  companyId?: number;
  companyName?: string;
  countryId?: number;
  country?: Country;
  countryName?: string;
}

And here is the userProfile.service

  getUserProfiles(): Observable<UserProfile[]> {
    return this.http.get<UserProfile[]>(
      this.baseUrl + 'userprofiles/getuserprofiles'
    );
  }

In the template I've used ngFor to iterate through the Observable with an async pipe:

<mat-form-field
  fxFlex="22"
  appearance="outline"
  *ngIf="applicants$ | async as applicants"
>
  <mat-label>Applicant</mat-label>
  <mat-icon matPrefix>person</mat-icon>
  <mat-select
    placeholder="Applicant"
    name="applicant"
    [(ngModel)]="this.searchPurchaseOrder.applicantId"
  >
    <mat-option *ngFor="let ap of applicants" [value]="ap.id">
      ap.fullName
    </mat-option>
  </mat-select>
</mat-form-field>

However, here is the error I am getting: enter image description here

Here is the data shape from the store. It is being populated, so there is no problem there: enter image description here

like image 889
Efron A. Avatar asked Dec 31 '22 03:12

Efron A.


2 Answers

You will need to use the keyvalue pipe to loop through your object as if it were an array.

https://angular.io/api/common/KeyValuePipe

<div *ngFor="let applicant of applicants | keyvalue">
  {{applicant.key}}:{{applicant.value}}
</div>

This can work with the async pipe too, e.g:

<div *ngFor="let item of (object$ | async) | keyvalue">
like image 78
Matt Saunders Avatar answered Jan 05 '23 18:01

Matt Saunders


The problem here is that you are trying to use ngFor to loop through an object (Which is not possible). If you can post a preview of the response from the network tab it would be helpful to get a better idea about the matter.

like image 22
Charith Hettiarachchi Avatar answered Jan 05 '23 18:01

Charith Hettiarachchi