1

I am trying to use ngFor loop to get data from my array. I get this

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

How can I fix it?

how i get data

if (this.resources$) {
  this.resources$.subscribe(result => {
    if (result)  {
      this.resourcesList = <Resources[]>result;

      this.showResources = true;
    }
  });



export interface Resources {
 Id: number;
 Name: string; 
}

I am trying to use it like that

<div *ngFor="let res of resourcesList">{{res.Name}}</div>
  • 1
    syntax looks correct. Please check the content of the resourceslist variable. set a breakpoint or display: {{ resourcesList | json }} – Marcel Hoekstra Jun 07 '19 at 08:42
  • 1
    Maybe your resourcesList doesn't get set, because you never get into the wrapping if condition? Could you please try to log this? And by the way - You should be able to DOM subscribe with async pipe directly to your resources$ Observable: *ngFor="let res of resources$ | async". Using this you dont need anything of your .ts logic, just the resources$ observable. – Severin Klug Jun 07 '19 at 08:43
  • Check what your backend is returning, make sure it's an array, not an object containing an array. – Ala Abid Jun 07 '19 at 08:45
  • result = {Resources: Array(1), ItemCount = 1} – Bartosz Stępak Jun 07 '19 at 08:59

3 Answers3

2

you can use keyvalue pipe if you want to iterate over a object.

<div *ngFor="let res of resourcesList | keyvalue">
    {{res | json}}
</div>

Yash Rami
  • 2,276
  • 1
  • 10
  • 16
1
  1. resourcesList must be an array.
  2. Initialize resourcesList to an empty array i.e public resourcesList : any = []
  3. make sure the response send by this.resources$.subscribe(...) is array of object
  4. change <div *ngFor="let res of resourcesList">{{res?.Name}}</div>
programoholic
  • 4,830
  • 5
  • 20
  • 59
1

According to your comment on another answer, your backend returns an object in this format:

{
  "ItemsCount": 1,
  "Resources": [
    {
      "Name": "Garaż wolnostojący 1",
      "DerivedFullName": "Garaż wolnostojący 1",
      "ParentId": 0,
      "IsLinkPossible": true,
      "Children": [],
      "Code": "",
      "Path": "Garaż wolnostojący 1",
      "Description": "",
      "Id": 186973
    }
  ]
}

Which is not an array, the array is in response.Resources, so doing this:

this.resourcesList = <ResourcesDetailsToIssueResponse[]>result.Resources;

will probably do the job for you.

Ala Abid
  • 2,256
  • 23
  • 35