【问题标题】:TypeError: Cannot convert undefined or null to object in Ionic/AngularTypeError:无法将 undefined 或 null 转换为 Ionic/Angular 中的对象
【发布时间】: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


    【解决方案1】:

    您对可能导致问题的分析似乎是正确的。

    当函数需要一个对象时,尝试传递nullundefined 值时,您将遇到TypeError: Cannot convert undefined or null to object。在您的情况下,有几个代码 sn-ps 可能会导致错误:

    Object.keys(this.receiveData)
    Object.values(this.receiveData)
    Object.values(this.cryData)
    

    这就是我在导出类代码 private receiveData: any; 中定义它的方式,我尝试将 any 更改为 any[] 和字符串,我尝试了一些其他方法来修复它但没有成功

    仅仅为变量指定 Typescript 类型并不能解决问题。您实际上需要确保传递给Object.keys()Object.values() 的值既不是null 也不是undefined

    【讨论】:

      【解决方案2】:

      看起来您只需要检查您是否在此处获得 undefined / null

       this._data.getPricesFull()
            .subscribe(res => {
              if (!res || !res.DISPLAY) return;
              this.receiveData = res.DISPLAY;
            ...
      

      【讨论】:

        猜你喜欢
        • 2021-12-07
        • 2022-10-24
        • 2023-01-20
        • 2019-10-25
        • 1970-01-01
        • 2022-06-16
        • 2015-06-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多