我正在嘗試設定加密貨幣實時市場價格。但它沒有顯示。我只在我的 chrome 開發者控制臺中看到這個錯誤。
錯誤型別錯誤:無法將未定義或空值轉換為物件
我的組件.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,我嘗試了一些其他方法來修復它,但沒有成功,現在已經與此作斗爭了幾天。有人應該幫助我。
uj5u.com熱心網友回復:
您對可能導致問題的分析似乎是正確的。
您將遇到TypeError: Cannot convert undefined or null to object while trying to pass nullor undefinedvalues when the function requires an object。在您的情況下,有幾個代碼片段可能會導致錯誤:
Object.keys(this.receiveData)
Object.values(this.receiveData)
Object.values(this.cryData)
這就是我在匯出類代碼 private receiveData: any; 中定義它的方式,我嘗試將 any 更改為 any[] 和字串,我嘗試了一些其他方法來修復它但沒有成功
簡單地為變數指定 Typescript 型別并不能解決問題。您實際上需要確保您傳遞給的值Object.keys()是Object.values()既不是null也不是undefined。
uj5u.com熱心網友回復:
看起來你只需要檢查你沒有在這里得到undefined/null值
this._data.getPricesFull()
.subscribe(res => {
if (!res || !res.DISPLAY) return;
this.receiveData = res.DISPLAY;
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/424043.html
