久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

獲取 SyntaxError:詞法聲明不能出現(xiàn)在單語句上下文

Getting SyntaxError: Lexical declaration cannot appear in a single-statement context(獲取 SyntaxError:詞法聲明不能出現(xiàn)在單語句上下文中)
本文介紹了獲取 SyntaxError:詞法聲明不能出現(xiàn)在單語句上下文中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我的代碼的 let 部分產(chǎn)生了一個錯誤,我通過將 client.login 移動到底部來弄清楚為什么機(jī)器人無法啟動新錯誤包括它只是發(fā)送垃圾郵件無效的郵政編碼.請遵循以下格式:_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');

推薦答案

你不能在像 if 這樣的語句之后使用詞法聲明(constlet)elsefor 等沒有塊 ({}).改用這個:

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
});

<小時>

編輯第二個問題

您需要檢查郵件是否由機(jī)器人發(fā)送,以便它忽略他們發(fā)送的所有郵件,包括無效郵政編碼"郵件:


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
});

否則,無效郵政編碼"消息將觸發(fā)機(jī)器人發(fā)送另一個無效郵政編碼"消息,因?yàn)闊o效郵政編碼"顯然不是有效的郵政編碼.

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) 因?yàn)?isNaN 將其輸入強(qiáng)制轉(zhuǎn)換為一個數(shù)字,然后檢查它是否為 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

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

這篇關(guān)于獲取 SyntaxError:詞法聲明不能出現(xiàn)在單語句上下文中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機(jī)器人提及發(fā)出該機(jī)器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時的歡迎消息)
主站蜘蛛池模板: 亚洲精品日韩一区二区电影 | 成人免费区一区二区三区 | 国产精品欧美一区二区 | 波波电影院一区二区三区 | 久久免费精品视频 | 仙人掌旅馆在线观看 | 91观看 | 久久成人一区二区三区 | 国产免费一区 | 亚洲日本一区二区 | 国产精品一区二区在线免费观看 | 国产一区二区三区 | 亚洲香蕉在线视频 | 日本aa毛片a级毛片免费观看 | 中文字幕精 | 福利片一区二区 | 国产精品一区二区在线观看 | 日韩一二三区视频 | 毛片a级毛片免费播放100 | 亚洲国产精品久久人人爱 | 野狼在线社区2017入口 | 久久久91精品国产一区二区三区 | 亚洲中午字幕 | 99热.com| 国产精品毛片久久久久久 | 伊人超碰在线 | 羞羞的视频在线观看 | 亚洲精品视频一区二区三区 | 羞羞视频在线观免费观看 | aaa级片 | 国产成人精品高清久久 | 色视频www在线播放国产人成 | 久久久久久免费毛片精品 | 在线观看国产精品一区二区 | 久操亚洲 | 欧美日韩精品一区二区三区四区 | 久久天堂网 | 午夜成人在线视频 | 久久只有精品 | 欧美精品网| www,黄色,com |