首页 > 解决方案 > TypeError:无法读取未定义的属性“合格”

问题描述

我有这段 1 年前的代码,现在需要运行它。欢迎任何可能的猜测。任何信息都会有所帮助,谢谢。

function getPrices(){
  let url = 'https://url/api/Prices/v4?key=' + key;
  var massive;
  let quality = '5';
  request({url:url, json:true},  function(err, res, body){
        if (err) console.log(err);

        massive = body.response.items;

        console.log('Prices received')
        for(let i = 0; i < data.length; i += 1){

          let get_json = other.urltojson(data[i].ApiLink);
          let particle = get_json.particle
          let name = data[i].BuyLink;
              name = name.replace(/(\/Eligible).*/, '');
              name = name.replace(/.*(\/)/, '');
              name = name.replace(/\%20/g, ' ');
              name = name.replace(/\%27/g, '\'')

          let comPrice = massive[name].prices[quality].Eligible.Suitable[definition].value;
          data[i].BuyPrice = comPrice;
        }
      });
}

错误日志:

          let comPrice = massive[name].prices[quality].Eligible.Suitable[definition].value;
                                                       ^

TypeError: Cannot read property 'Eligible' of undefined

标签: javascripttypeerror

解决方案


这个错误意味着massive[name].prices[quality]给出未定义的。

我不知道您的数据结构如何,但可能是您将您的数据存储quality为字符串而不是整数

编辑:

massive[name].prices[quality]如果is进行后备可能对您来说是有意义的undefined


推荐阅读