如何修复TypeError:无法读取未定义的属性'prices'

时间:2019-06-19 07:00:19

标签: javascript node.js typeerror

我正在制作一个tf2交易机器人。它给我一个错误,因为未定义data [baseName],但baseName将是一个tf2项目,而data是所有tf2项目的列表。 数据的一部分是:

{ 'Purity Fist':
   { defindex: [ 524 ],
     prices: { '0': [Object], '1': [Object], '6': [Object] } },
  'Black Rose':
   { defindex: [ 727 ], prices: { '0': [Object], '6': [Object] } },
  Bat:
   { defindex: [ 190, 0 ],
     prices:
      { '0': [Object],
        '3': [Object],
        '4': [Object],
        '6': [Object],
        '11': [Object] } },
  'Ellis\' Cap':
   { defindex: [ 263 ], prices: { '0': [Object], '6': [Object] } },
  Batsaber:
   { defindex: [ 30667 ],
     prices: { '0': [Object], '6': [Object], '11': [Object] } },
  'Prinny Machete':
   { defindex: [ 30758 ],
     prices: { '0': [Object], '1': [Object], '6': [Object] } },
  'Righteous Bison':
   { defindex: [ 442 ],
     prices:
      { '1': [Object], '6': [Object], '11': [Object], '14': [Object] } },
  'Sun-on-a-Stick':
   { defindex: [ 349 ],
     prices:
      { '1': [Object], '3': [Object], '6': [Object], '14': [Object] } },
  Grandmaster: { defindex: [ 1067 ], prices: { '1': [Object] } },
  'Mister Bubbles':
   { defindex: [ 1089 ], prices: { '1': [Object], '6': [Object] } },
  'Fan O\'War':
   { defindex: [ 355 ],
     prices:
      { '1': [Object],
        '3': [Object],
        '6': [Object],
        '11': [Object],
        '14': [Object] } },
  'Conniver\'s Kunai':
   { defindex: [ 356 ],
     prices:
      { '1': [Object],
        '3': [Object],
        '6': [Object],
        '11': [Object],
        '14': [Object] } },
var data = {

};
var currencies = {

};
requestify.get('https://backpack.tf/api/IGetPrices/v4?raw=1&since=0&key=5cf17c256780725011449df2')
    .then(function(response) {

    data = response.getBody().response.items;
    console.log(data);
  }
);
requestify.get('https://backpack.tf/api/IGetCurrencies/v1?key=5cf17c256780725011449df2')
  .then(function(response) {

      currencies = response.getBody().response.currencies;
  }
);

function toRef(keys, high) {
    if (high) {
        if (currencies.keys.price.value_high != undefined){
        return currencies.keys.price.value_high * keys
        } else {
            return currencies.keys.price.value * keys
        }
    } else {
        return currencies.keys.price.value * keys
    }
}
function getPrice(item, high) {
    var name = item.market_name;
    var quality = item.tags[0].name;
    var baseName = name.replace(quality + " ", "");
    var qualityId = itemQualities[quality];
    var isCraftable = true;
    for (i = 0;i < item.descriptions.length;i++) {
        if (item.descriptions[i].value == '( Not Usable in Crafting )') {
            isCraftable = false;
        }
    }

    if (high) {
        if (data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].currency == "keys") {
            return toRef(data[baseName]['prices'][qualityId.toString()]['Tradable'][isCraftable.toString()][0].value_high, true);
        } else {
            return data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].value_high;
        }
    } else {
        if (data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].currency == "keys") {   
            return toRef(data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].value, false);
        } else {
            return data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].value;
        }
    }

}

它应该返回data [baseName] .prices [qualityId.toString()]。Tradable [isCraftable.toString()] [0] .value的值,但会出错

0 个答案:

没有答案