0

I have method to get data from back end to array

Here is method

getUtilities(): void {
    // DropdownHelpers.fillDropdownOptions(
    //     this._propertyService.getMarketingInformationUtilityTypes(),
    //     this.utilities
    // );
    this._propertyService.getMarketingInformationUtilityTypes().subscribe(result => {
        this.utilities = result.items;
        console.log(this.utilities);
    });
}

and here is what I see in the console

enter image description here

But when I try to do *ngFor="let utility of utilities"

I got

MarketingEditComponent.html:38 ERROR Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Where is my problem? Data is returning to array

5
  • 1
    this.utilities maybe undefined before the API call - at that time it's not an array. Add *ngIf before the loop to check if it is an array OR initialize it to an empty array when you define it. Commented Apr 25, 2020 at 6:57
  • the initial value is an empty array utilities: any[] = []; @David Commented Apr 25, 2020 at 7:05
  • The console.log comes from edit-property-market..ion.component.ts, but the error message comes from MarketingEditComponent.html. Can you post your HTML and TS for MarketingEditComponent. Commented Apr 25, 2020 at 7:10
  • it's same component @KurtHamilton Commented Apr 25, 2020 at 7:13
  • @EugeneSukh So... can you post it? Commented Apr 25, 2020 at 7:21

2 Answers 2

1

this.utilities maybe undefined before the API call - at that time it's not an array.

Solutions:

  • Add *ngIf before the loop to check if it is an array in the template.

OR

  • Initialize it to an empty array when you define it i.e this.utilities = [];
Sign up to request clarification or add additional context in comments.

4 Comments

utilities is loading at ngOnInit(), I call ngFor at the modal show. So it can be null. Maybe the problem in data structure?
utilities: any[] = []; at start of TS file
Or like this utilities: SelectItem[] = [];
Is is getting redefined somewhere else in your component? Can you share the relevant snippets from the template as well?
0

when you define utilities declare it as

utilities: any; or utilities:any = [];

and in your use *ngIf before you iterate something like

<div *ngIf="utilities"> 
<div *ngFor="let util of utilities">
{{util.OptionDto.name}} {{util.OptionDto.id}} 
</div>
</div>

Nothing new from what they've said earlier. If your still confused provide me a stackblitz.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.