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