TypeError: Cannot convert undefined or null to object in Ionic/Angular
TypeError: Cannot convert undefined or null to object in 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);
这就是我在导出 class 代码 private receiveData: any; 中定义它的方式,我尝试更改 any 到 any[] 和 string,我尝试了一些其他方法来修复它但没有成功,一直在与这几天了。有人应该好心帮助我。
您对可能导致问题的分析似乎是正确的。
您将遇到 TypeError: Cannot convert undefined or null to object while trying to pass null
or undefined
values while the function expects an 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;
...
我正在尝试设置加密货币实时市场价格。但它没有显示。我只在我的 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);
这就是我在导出 class 代码 private receiveData: any; 中定义它的方式,我尝试更改 any 到 any[] 和 string,我尝试了一些其他方法来修复它但没有成功,一直在与这几天了。有人应该好心帮助我。
您对可能导致问题的分析似乎是正确的。
您将遇到 TypeError: Cannot convert undefined or null to object while trying to pass null
or undefined
values while the function expects an 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;
...