【发布时间】:2022-02-09 04:19:35
【问题描述】:
我正在尝试设置加密货币实时市场价格。但它没有显示。我只在我的 chrome 开发者控制台中看到了这个错误。
ERROR TypeError: 无法将 undefined 或 null 转换为对象
我的组件.ts
ngOnInit() {
this.refreshData();
}
refreshData(reset:boolean = false) {
// Reset table index to 1
if (reset) {
this._data._previousIndex = 1;
}
// Set table page index and size to previous resevered data
if (this._data._previousIndex !== null && this._data._previousPageSize !== null) {
this._current = this._data._previousIndex;
this._pageSize = this._data._previousPageSize;
this._sortMap.name = this._data._previousSortMapName;
this._sortMap.symbol = this._data._previousSortMapSymbol;
//console.log("reserve data called");
}
this._loading = true;
// Sort dataset before get
if (this._sortName !== null || this._sortValue !== null) {
this._data.sortData(this._sortName, this._sortValue);
//console.log("sort method called");
}
this.cryData = [];
this.cryptoLastPrices = [];
this.cryptoPriceCompare = [];
this.cryptoNames = this._data.getNamesFull();
this.cryptoImages = this._data.getImagesFull();
this._placeHolderSafe = this._sanitizer.bypassSecurityTrustUrl(this._placeholderBase64);
this._data.getPricesFull()
.subscribe(res => {
this.receiveData = res.DISPLAY;
//console.log(this.receiveData);
let coinKeys: any = Object.keys(this.receiveData);
let coinValues: any = Object.values(this.receiveData);
// Price compare first time check
if (this.cryptoLastPrices.length === 0) {
for (let _i = 0; _i < coinKeys.length; _i++) {
let _currentPrice = parseFloat((coinValues[_i].USD.PRICE).substring(2).replace(/,/g, ''));
this.cryptoLastPrices[_i] = _currentPrice;
this.cryptoPriceCompare[_i] = _currentPrice - this.cryptoLastPrices[_i];
}
} else {
for (let _i = 0; _i < coinKeys.length; _i++) {
this.cryptoPriceCompare[_i] = (parseFloat((coinValues[_i].USD.PRICE).substring(2).replace(/,/g, '')) -
this.cryptoLastPrices[_i]);
}
}
//console.log(this.cryptoLastPrices);
for (let _i = 0; _i < coinKeys.length; _i++) {
this.cryData[coinKeys[_i]] = {
image: this.cryptoImages[_i],
name: this.cryptoNames[_i],
symbol: coinKeys[_i],
price: coinValues[_i].USD.PRICE,
marketCap: coinValues[_i].USD.MKTCAP,
change24Num: parseFloat((coinValues[_i].USD.CHANGE24HOUR).substring(2).replace(/,/g, '')),
priceCompare: this.cryptoPriceCompare[_i]
}
this.cryptoLastPrices[_i] = parseFloat((coinValues[_i].USD.PRICE).substring(2).replace(/,/g, ''));
this.cryptos = JSON.parse(JSON.stringify(Object.values(this.cryData)));
}
//console.log(Object.values(this.cryData));
this._loading = false;
this.setTimer();
});
}
我认为错误在于这些行
let coinKeys: any = Object.keys(this.receiveData);
let coinValues: any = Object.values(this.receiveData);
这就是我在导出类代码private receiveData: any;中定义它的方式,我尝试将any更改为any[]对于string,我尝试了其他一些方法来修复它,但没有成功,现在已经为此奋斗了几天。有人应该帮助我。
【问题讨论】:
标签: angular ionic-framework cryptography