getData() {
this.http.get(`https://api.covid19api.com/total/country/${this.country}/status/confirmed?from=2020-04-01T00:00:00Z&to=2020-05-01T00:00:00Z`).subscribe(res => {
console.log('Res: ', res);
this.chartData[0].data = [];
this.chartLabels = [];
for (let entry of res) {
// splitting Date from Date and Time that was provided from dataset
this.chartLabels.push(entry.Date.split('T')[0]);
this.chartData[0].data.push(entry['Cases']);
}
console.log('data: ', this.chartData);
});
}
大约2天前,让此函数从API调用数据,但是当我今天再次尝试“离子服务”该项目时,我遇到了编译错误。之前曾出现此错误,但我寻求帮助,我的一位朋友告诉我,这是TS的侧边错误,可以忽略。
该错误似乎来自“ res”对象。我是一名新编码员,所以简单的说明将非常有帮助!
我很困惑,请问是否需要更多信息。
谢谢!
答案 0 :(得分:0)
问题在您的放置对象中,检查该对象
//sample object and it goes iterator
let res = [1,2,3,4,4];
for (let entry of res) {
console.log('data: ', entry);
}
答案 1 :(得分:0)
getData() {
this.http.get(`https://api.covid19api.com/total/country/${this.country}/status/confirmed?from=2020-04-01T00:00:00Z&to=2020-05-01T00:00:00Z`).subscribe((res:any) =>
{
console.log('Res: ', res);
this.chartData[0].data = [];
this.chartLabels = [];
for (let entry of res) {
// splitting Date from Date and Time that was provided from dataset
this.chartLabels.push(entry.Date.split('T')[0]);
this.chartData[0].data.push(entry['Cases']);
}
console.log('data: ', this.chartData);
});
}
将第2行从“ .subscribe(res => {” 至 “ .subscribe((res:any)=> {”似乎有效。