1
votes

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

3
The data returned from the service is not an array. It might have a body with an array inside of it, but if you show some example of the real output, we might be able to help you further.Silvermind
I don't know if that call to the api works or not. I have not tested your Stackblitz. You should add the response to your question instead.Silvermind

3 Answers

2
votes

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

0
votes

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>
0
votes

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);
});

}