【发布时间】:2020-02-23 16:06:26
【问题描述】:
这是我的代码: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 => {
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 TypeError: res.Data.map is not a function 当我试图显示折线图时。
我希望有人可以帮助我。谢谢各位!
我只是 ionic 家伙的新手,如果有人要帮助我,我将不胜感激
【问题讨论】:
-
这意味着
res['Data']不是一个数组。您是否尝试过将该值记录到控制台以查看它是什么?此外,如果要执行this.result = result之类的操作,最好使用tap而不是map。这表示您希望执行一个操作作为副作用而不返回不同的值。 -
另一个仅供参考 - 最好使用
switchMap而不是创建嵌套订阅。这不是问题的原因,只是提醒一下。
标签: angular ionic-framework ionic4