Attempting to compile a list of data fetched from an endpoint, I receive 10 pieces of data and aim to utilize *ngFor to exhibit them. The data is successfully received in the correct order, but an error arises:
ERROR Error: "Cannot find a differ supporting object '[object Promise]' of type 'object'. NgFor only supports binding to Iterables such as Arrays."
However, it seems that utilizing JSON in *ngFor is possible with recent Angular versions.
JSON received: https://pastebin.com/TTn0EqSS
app.component.html
<div [class.app-dark-theme]="true">
<mat-sidenav-container fullscreen class="sidenav-container">
<mat-toolbar class="toolbar">
Coin Market Cap 3rd party api app
</mat-toolbar>
<mat-card>
<mat-card-header>
<mat-card-title>CryptoCurrency Market Overview</mat-card-title>
<mat-card-subtitle>Top 15 current currencies.</mat-card-subtitle>
</mat-card-header>
<mat-card-content class="currency-listings">
<div *ngIf="finishedLoading">
<mat-grid-list cols="1" rowHeight="2:1">
<mat-grid-tile *ngFor="let currency of currenciesJson; let i = index" (click)="selectCurrency(i)">
{{currency.data[i].name}}
</mat-grid-tile>
</mat-grid-list>
test
test
test
</div>
</mat-card-content>
</mat-card>
<!-- (click)="showInfo(true)" -->
<mat-card *ngIf="displayInfo">
test
</mat-card>
</mat-sidenav-container>
</div>
coin-market-cap.service.ts
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class CoinMarketCapService
{
key = "REDACTED";
apiUrl = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/';
constructor(public http: HttpClient) { }
getCurrencies(totalCurrencies: number)
{
let promise = new Promise((resolve, reject) => {
let url = this.apiUrl + "listings/latest?limit=" + totalCurrencies + "&CMC_PRO_API_KEY=" + this.key;
this.http.get(url)
.toPromise()
.then(
res => {
console.log(res);
resolve();
});
})
return promise;
}
getCurrency(currencyId: number)
{
console.log("in getcurrency");
let url = this.apiUrl + "info?id=" + currencyId + "&CMC_PRO_API_KEY=" + this.key;
console.log(url);
return this.http.get(url);
}
}
app.component.ts
import { Component } from '@angular/core';
import { CoinMarketCapService } from '../services/coin-market-cap.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent
{
currenciesJson = {};
displayInfo = false;
finishedLoading = false;
constructor(private CoinMarketCapService: CoinMarketCapService) {}
ngOnInit()
{
console.log(this.currenciesJson);
this.currenciesJson = this.CoinMarketCapService.getCurrencies(10)
.then(res =>
this.finishedLoading = true
)
console.log(this.currenciesJson);
console.log("exiting ngoninit");
}
selectCurrency(currencyId: number)
{
console.log(currencyId);
let currencyObject = this.CoinMarketCapService.getCurrency(currencyId);
}
showInfo ( showInfo: boolean )
{
this.displayInfo = showInfo;
}
}