正在获取SyntaxError:词法声明不能出现在单语句上下文中 [英] Getting SyntaxError: Lexical declaration cannot appear in a single-statement context

查看:302
本文介绍了正在获取SyntaxError:词法声明不能出现在单语句上下文中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码的让步部分产生了一个错误,我弄清楚了为什么机器人无法通过移动客户端来启动。登录到底部的新错误包括它只是发送无效的邮政编码。请遵循以下格式:_weather< ; #####>,即使您输入了邮政编码

The let part of my code is producing an error I figured out why the bot wouldn't start by moving the client.login to the bottom new error includes it just spamming "Invalid Zip Code. Please follow the format: _weather <#####>" even if you put in the zipcode

client.on("message", (message) => {
    if (message.content.includes("_weather") && message.author.bot === false)
        let zipCode = message.content.split(" ")[1];
    if (zipCode === undefined || zipCode.length != 5 || parseInt(zipCode) === NaN) {
        message.channel.send("`Invalid Zip Code. Please follow the format: _weather <#####>`")
            .catch(console.error);
        return;
    } else {
        fetch(`https://openweathermap.org/data/2.5/weather?zip=${zipCode},us&appid=439d4b804bc8187953eb36d2a8c26a02`)
            .then(response => {
                return response.json();
            })
            .then(parsedWeather => {
                if (parsedWeather.cod === '404') {
                    message.channel.send("`This zip code does not exist or there is no information avaliable.`");
                } else {
                    message.channel.send(`

        The Current Weather
        Location: ${parsedWeather.name}, ${parsedWeather.sys.country}
        Forecast: ${parsedWeather.weather[0].main}
        Current Temperature: ${(Math.round(((parsedWeather.main.temp - 273.15) * 9 / 5 + 32)))}° F
        High Temperature: ${(Math.round(((parsedWeather.main.temp_max - 273.15) * 9 / 5 + 32)))}° F
        Low Temperature: ${(Math.round(((parsedWeather.main.temp_min - 273.15) * 9 / 5 + 32)))}° F
        `);

                }
            });
    }
});
client.login('token');


推荐答案

您不能使用词法声明(<$ c像 if const 和 let ) > else , for 等,无障碍( {} )。改用此方法:

You can't use lexical declarations (const and let) after statements like if, else, for etc. without a block ({}). Use this instead:

client.on("message", (message) => {
    // declares the zipCode up here first
    let zipCode
    if (message.content.includes("_weather") && message.author.bot === false)
        zipCode = message.content.split(" ")[1];
    // rest of code
});






编辑第二个问题



您需要检查消息是否是由漫游器发送的,以便它将忽略它们发送的所有消息,包括无效的邮政编码消息:


Edit for 2nd question

You need to check if the message was sent by a bot so that it will ignore all messages sent by them, including the 'Invalid Zip Code' message:

client.on("message", (message) => {
    if (!message.author.bot) return;
    // rest of code
});

否则,无效邮政编码消息将触发漫游器发送另一个无效邮政编码

Without that, the 'Invalid Zip Code' message would trigger the bot to send another 'Invalid Zip Code' message as 'Invalid Zip Code' is obviously not a valid zip code.

同样,更改 parseInt(zipCode)=== NaN Number.isNaN(parseInt(zipCode)) NaN === NaN 在JS中由于某种原因是 false ,因此您需要使用 Number.isNaN 。您还可以只执行 isNaN(zipCode),因为 isNaN 将其输入强制为数字,然后检查其是否为 NaN

Also, change parseInt(zipCode) === NaN to Number.isNaN(parseInt(zipCode)). NaN === NaN is false for some reason in JS, so you need to use Number.isNaN. You could also just do isNaN(zipCode) because isNaN coerces its input to a number and then checks if it's NaN.

console.log(`0 === NaN: ${0 === NaN}`)
console.log(`'abc' === NaN: ${'abc' === NaN}`)
console.log(`NaN === NaN: ${NaN === NaN}`)
console.log('')
console.log(`isNaN(0): ${isNaN(0)}`)
console.log(`isNaN('abc'): ${isNaN('abc')}`)
console.log(`isNaN(NaN): ${isNaN(NaN)}`)
console.log('')
console.log(`Number.isNaN(0): ${Number.isNaN(0)}`)
console.log(`Number.isNaN('abc'): ${Number.isNaN('abc')}`)
console.log(`Number.isNaN(NaN): ${Number.isNaN(NaN)}`)

尝试以下代码:

client.on("message", (message) => {
  if (message.content.includes("_weather") && !message.author.bot) {
    let zipCode = message.content.split(" ")[1];
    if (zipCode === undefined || zipCode.length != 5 || Number.isNaN(parseInt(zipCode))) {
      message.channel.send("`Invalid Zip Code. Please follow the format: _weather <#####>`")
        .catch(console.error);
      return;
    } else {
      fetch(`https://openweathermap.org/data/2.5/weather?zip=${zipCode},us&appid=439d4b804bc8187953eb36d2a8c26a02`)
        .then(response => {
          return response.json();
        })
        .then(parsedWeather => {
          if (parsedWeather.cod === '404') {
            message.channel.send("`This zip code does not exist or there is no information avaliable.`");
          } else {
            message.channel.send(`

        The Current Weather
        Location: ${parsedWeather.name}, ${parsedWeather.sys.country}
        Forecast: ${parsedWeather.weather[0].main}
        Current Temperature: ${(Math.round(((parsedWeather.main.temp - 273.15) * 9 / 5 + 32)))}° F
        High Temperature: ${(Math.round(((parsedWeather.main.temp_max - 273.15) * 9 / 5 + 32)))}° F
        Low Temperature: ${(Math.round(((parsedWeather.main.temp_min - 273.15) * 9 / 5 + 32)))}° F
        `);

          }
        });
    }
  }
})






编辑3




Edit 3

if (message.content.startsWith("_weather") && !message.author.bot)

这篇关于正在获取SyntaxError:词法声明不能出现在单语句上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆