【问题标题】:Unexpected token ; when defining Boolean variable意外的标记 ;定义布尔变量时
【发布时间】:2019-06-18 20:01:57
【问题描述】:

我正在设置一个可以进行价格检查的 TF2 交易机器人。在定义布尔值是否以键计价时出现错误。

我尝试在 if 语句中将 isKeys 替换为 data[baseName].prices[qualityId.toString()].Tradable[craftable[isCraftable.toString()][0].currency == "keys" 但得到if 语句中右括号的错误。

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["Australium Tomislav"].prices["11"].Tradable.Craftable);
  }
);
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;
    var isKeys = data[baseName].prices[qualityId.toString()].Tradable[craftable[isCraftable.toString()][0].currency == "keys"; // Error here
    for (i = 0;i < item.description.length;i++) {
        if (item.description[i].value == '( Not Usable in Crafting )') {
            isCraftable = false;
        }
    }

    if (high) {
        if (isKeys) {
            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 (isKeys) {   
            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];
        }
    }

}

`

G:\BOT\bot.js:106 var isKeys = data[baseName].prices[qualityId.toString()].Tradable[craftable[isCraftable.toString()][0].currency == "keys"; ^

SyntaxError: Unexpected token ;

是我得到的错误

【问题讨论】:

  • 您在该表达式中缺少]

标签: javascript node.js steam unexpected-token


【解决方案1】:

TL;DR:您在错误的行中缺少]。你在下面的if(high){...} 行中有额外的]

您在该行中缺少方括号]var isKeys = ... 正如其他答案所暗示的那样。 现在,我们不知道数据结构,所以它可能是,

data[baseName]
    .prices[qualityId.toString()]
    .Tradable[craftable[isCraftable.toString()][0].currency*]*

data[baseName]
    .prices[qualityId.toString()]
    .Tradable[craftable[isCraftable.toString()][0]*]*.currency

还有,

行上有多余的方括号,

if (high) {
        if (isKeys) {
        /*--here>>*/return toRef(data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].value_high, true);
        } else {
        /*--here>>*/return data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].value_high;
        }
    } else {
        if (isKeys) {
        /*--here>>*/ return toRef(data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].value, false);
        } else {
        /*--here>>*/return data[baseName].prices[qualityId.toString()].Tradable[isCraftable.toString()][0].value;
        }
    }

同样,我们不知道确切的数据结构。

【讨论】:

    【解决方案2】:

    你错过了 Tradable 的方括号

    var isKeys = data[baseName].prices[qualityId.toString()].Tradable[craftable[isCraftable.toString()]][0].currency == "keys";
    

    【讨论】:

      【解决方案3】:

      在该行中缺少方括号关闭 (])。

      您的线路是:

      var isKeys = data[baseName].prices[qualityId.toString()].Tradable[craftable[isCraftable.toString()][0].currency == "keys"; // Error here
      

      您在.Tradable[ 处打开一个括号,但直到该行末尾才关闭。 编译器需要],但找到;

      我不熟悉您使用的 API,但我想以下方法可以解决错误:

      var isKeys = data[baseName].prices[qualityId.toString()].Tradable[craftable[isCraftable.toString()][0].currency == "keys"]; // << Notice the bracket before your semicolon
      

      【讨论】:

        猜你喜欢
        • 2012-09-28
        • 1970-01-01
        • 2018-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-31
        相关资源
        最近更新 更多