I am trying to load an array from an API and display it in a table with Angular 5.
Sounds simple heres my code:
import { HttpClient } from '@angular/common/http';
export class CryptreviewComponent implements OnInit {
public results:Review[] = [];
constructor(private http: HttpClient) { }
ngOnInit() {
this.http.get('http:...url..').subscribe(data => {
console.log(typeof data);
console.log(typeof this.results);
this.results = data as Review[];
console.log(typeof this.results);
console.log(this.results);
},
err => {
console.log(err);
} );
}
}
interface Review{
coin_id:string;
user_id:number;
name:string;
reach_remark:string;
reach_score:number;
security_remark:string;
security_score:number;
dev_remark:string;
dev_score:number;
transparency_remark:string;
transparency_score:number;
fungibility_remark:string;
fungibility_score:number;
coin_distribution_remark:string;
coin_distribution_score:number;
privacy_remark:string;
pricavy_score:number;
hardened_remark:string;
hardened_score:number;
features_remark:string;
features_score:number;
remarks:string;
links:string;
published:number;
verification_status:number;
}
The view:
<table>
<tr *ngFor="let review of results">
<td>
{{review.coin_id}}
</td>
</tr>
</table>
This is the result I get:
string -> console.log(typeof data);
object -> console.log(typeofthis.results);
string -> console.log(typeof this.results);
[{"coin_id":"BTC","user_id":13,"name":"","reach_remark":"","reach_score":100,"security_remark":"","security_score":93,"dev_remark":"","dev_score":70,"transparency_remark":"","transparency_score":81,"fungibility_remark":"","fungibility_score":30,"coin_distribution_remark":"","coin_distribution_score":66,"privacy_remark":"","privacy_score":71,"hardened_remark":"","hardened_score":100,"features_remark":"","features_score":34,"remarks":"","links":"","published":0,"verification_status":0}]
-> console.log(this.results);
ERROR Error: Error trying to diff '[{"coin_id":"BT...'. Only arrays and iterables are allowed
The result I get seems to be valid JSON but it just does not want to become an array.
So far I have tried: JSON.parse(data), data.json()