TypeError:无法在 Ionic/Angular 中将未定义或空转换为对象
我正在尝试设置加密货币实时市场价格。但它没有显示。我只在我的 chrome 开发者控制台中看到此错误。
ERROR TypeError: Cannot convert undefined or null to object
mycomponent.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 acceptData: any; 中定义它的方式,我尝试将 any 更改为 any[] 和 string ,我尝试了其他一些方法来修复它,但没有成功,已经为此奋斗了好几天。有人应该帮助我。
您对可能导致问题的分析似乎是正确的。
当函数需要一个 Object 时,尝试传递
null
或
undefined
值时,您会遇到
TypeError:无法将 undefined 或 null 转换为 object
。在您的情况下,有几个代码片段可能会导致错误:
Object.keys(this.receiveData)
Object.values(this.receiveData)
Object.values(this.cryData)
This is how i defined it in export class code private receiveData: any;, i've tried changing any to any[] and to string, i've tried some few other method to fix it but didn't work out
简单地为变量指定 Typescript 类型并不能解决问题。您实际上需要确保传递给
Object.keys()
和
Object.values()
的值既不是
null
也不是
undefined
。
看起来你只需要检查一下这里是否获取了
undefined
/
null
值
this._data.getPricesFull()
.subscribe(res => {
if (!res || !res.DISPLAY) return;
this.receiveData = res.DISPLAY;
...