Sandy
Sandy

Reputation: 321

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

I'm getting angular error message as : Error: Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays.

Based on the error message I understood, it's coming because I'm getting my API response as an object and trying to bind through array *ngFor.

I tried a few workarounds like an async pipe but that also didn't work.

Can someone help me with what mistake am I doing?

example.service.ts

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable()
export class BitcoinService {
  constructor(private _httpClient: HttpClient) {}

  // API endpoint
  apiURL = 'https://api.coindesk.com/v1/bpi/currentprice.json';

  // Create method
  coinData(): Observable<any> {
    return this._httpClient.get<any>(this.apiURL);
  }
}

example.component.ts

import { Component, OnInit } from '@angular/core';
import { BitcoinService } from './bitcoin.service';

@Component({
  selector: 'app-bitcoin',
  templateUrl: './bitcoin.component.html',
  styleUrls: ['./bitcoin.component.css']
})
export class BitcoinComponent implements OnInit {
  // Define Property
  showData;

  // Inject Service
  constructor(private _bitcoinService: BitcoinService) {}

  ngOnInit() {
    // call the method
    this.getCoinData();
  }

  // Create Method
  getCoinData() {
    this._bitcoinService.coinData().subscribe(res => {
      console.log(res);
      this.showData = res;
    });
  }
}

example.component.html

<!-- Test The Result -->
<ul *ngFor="let data of showData">
  <li> {{data.disclaimer}} </li>>
</ul>

Error Message

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

stackblitz link for reference https://stackblitz.com/edit/angular-simple-api-get-call-1?file=src/app/observable-examples/example-1/bitcoin/bitcoin.component.ts

Upvotes: 2

Views: 12116

Answers (4)

Steven Vaught
Steven Vaught

Reputation: 36

Not directly applicable to this scenario, but the question is highly visible for this error message, so this related solution might save someone some headache: Make sure you aren't trying to iterate over a template ref

I spent almost 30 minutes before I realized I had a #someArray in the template file as well as public someArray: Type[] = []; in the TypeScript file.

Upvotes: 0

eko
eko

Reputation: 40647

You shouldn't subscribe and use the async pipe at the same time. They do the same thing.

Also you forgot to initialize your array.

showData: any[] = [];

Fixed stackblitz

Upvotes: 2

Omkar Godse
Omkar Godse

Reputation: 81

Please define showData as array:

showData:any=[];

and store data like:

  getCoinData() {
this._bitcoinService.coinData().subscribe(res => {
  console.log(res);
  //this.showData = res;
  this.showData.push(res);
});

}

Upvotes: 0

Felix
Felix

Reputation: 1566

You can access your observable from your template. Like this:

example.component.ts

export class BitcoinComponent implements OnInit {
  showData: Observable<any>;

  constructor(private _bitcoinService: BitcoinService) {}

  ngOnInit() {
    this.showData = this._bitcoinService.coinData();
  }
}

example.component.html

<ul *ngFor="let data of showData | async">
  <li> {{data.disclaimer}} </li>>
</ul>

Upvotes: 0

Related Questions