Angular 我得到一个类似这样的错误:“quot;错误类型错误:res.Data.map不是函数;在Ionic4中
这是我的代码:home.page.tsAngular 我得到一个类似这样的错误:“quot;错误类型错误:res.Data.map不是函数;在Ionic4中,angular,ionic-framework,ionic4,Angular,Ionic Framework,Ionic4,这是我的代码:home.page.ts coinDetails(coin, index){ if(this.detailToggle[index]) this.detailToggle[index] = false; else { this.detailToggle.fill(false); this._data.getCoin(coin) .subscribe(res => { t
coinDetails(coin, index){
if(this.detailToggle[index])
this.detailToggle[index] = false;
else {
this.detailToggle.fill(false);
this._data.getCoin(coin)
.subscribe(res => {
this.details = res['DISPLAY'][coin]['USD'];
this.detailToggle[index]= true;
this._data.getChart(coin)
.subscribe(res => {
let coinHistory = res['Data'].map((a) => (a.close));
setTimeout(() => {
this.chart[index] = new Chart ('canvas' + index, {
type: 'line',
data: {
labels: coinHistory,
datasets: [{
data: coinHistory,
borderColor: '#3cba9f',
fill: false
}
]
},
options: {
tooltips: {
callbacks: {
label: function(tooltipItems,data) {
return "$" + tooltipItems.yLabel.toString();
}
}
},
reponsive: true,
legend: {
display: false
},
scales: {
xAxes: [{
display: false
}],
yAxes: [{
display: false
}],
}
}
});
}, 250);
});
});
}
}
}
data.service.ts
getChart(coin){
return this._http.get("https://min-api.cryptocompare.com/data/v2/histoday?fsym="+coin+"&tsym=USD&limit=30&aggregate=1&e=CCCAGG")
.pipe(map(result => this.result = result));
}
我只想在我的应用程序上显示折线图,在这个过程中,它一直显示错误类型ERROR:res.Data.map在我试图显示折线图时不是一个函数
我希望有人能帮助我。谢谢你们
我只是爱奥尼亚人的新手,如果有人能帮助我,我会非常感激。这意味着从
返回的对象上的数据属性。\u Data.getChart
是定义的,但不是数组。您应该尝试记录res
以查看属性的类型
我建议重构您的函数。这两个订阅可以使用forkJoin
并行运行,因为它们互不依赖
coinDetails(coin, index) {
if (this.detailToggle[index]) {
this.detailToggle[index] = false;
return;
}
this.detailToggle.fill(false);
// forkJoin to run 2 subscriptions in parallel
forkJoin([
this._data.getCoin(coin).pipe(
// let the individual calls update the state they are responsible for maintaining
tap(res => {
this.details = res['DISPLAY'][coin]['USD'];
this.detailToggle[index]= true;
})
),
this._data.getChart(coin).pipe(
tap(res => {
// log the result here to see what the data type is
console.log(res);
// ***** ERROR OCCURS HERE *****
this.coinHistory = res['Data'].map(a => a.close);
})
)
]).subscribe(() => {
// not sure why this is in a timeout, but have left it that way
setTimeout(() => {
// move update chart functionality into separate function for readability
// new function not shown as it is irrelevant to the problem
this.updateChart(index)
}, 250);
});
}
这意味着res['Data']
不是数组。您是否尝试将该值记录到控制台以查看它是什么?此外,如果要执行诸如this.result=result
之类的操作,最好使用点击
而不是映射
。这表示您希望在不返回其他值的情况下作为副作用执行操作。另一个供参考-最好使用switchMap
而不是创建嵌套订阅。这不是你问题的原因,只是提醒一下。