类型错误:无法读取未定义的属性“当前”

时间:2021-02-24 08:52:40

标签: javascript node.js discord command discord.js

我刚刚为 discord.js 创建了一个天气命令,它工作正常,但是如果我运行 p!weather 'not a city' 它会给我一个错误,我在我的代码中找不到任何错误,有谁知道如何修理它。我正在使用 slappy/discord.js。这是我的代码:

const Discord = require("discord.js");
const weather = require("weather-js");

module.exports = class WeatherCommand extends BaseCommand {
  constructor() {
    super('weather', 'fun', []);
  }

  async run(client, message, args) {
    if (!message.guild) return message.channel.send(`Try again if you are in a server channel!`);
    const city = message.content.split(" ").slice(1).join(" ")
    if (!city) return message.channel.send("I need a city to check :wink:")

    weather.find({search: city, degreeType: 'C'}, function(err, result) {
        if (err) {
            message.channel.send("**${arg}** Isnt inside my query, please check again")
            console.log(err.stack)
            return;
        }
        let url;
        if (result[0].current.skytext === "Mostly Sunny") url = "https://openclipart.org/image/2400px/svg_to_png/3367/ivak-Decorative-Sun.png"
        else if (result[0].current.skytext === "Mostly Cloudy" || result[0].current.skytext === "Cloudy") url = "https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Weather-heavy-overcast.svg/200px-Weather-heavy-overcast.svg.png"
        else if (result[0].current.skytext === "Partly Cloudy") url = "";
  
        var embed = new Discord.MessageEmbed()
        .setTitle(`Forecast for ${result[0].location.name}`, message.author.displayAvatarURL)
        .addField("Temperature", `**${result[0].current.temperature}ºC**`, true)
        .addField("Humidity", `**${result[0].current.humidity}%**`, true)
        .addField("Wind Speed", `**${result[0].current.windspeed.replace("mph", "ms/h")}**`, true)
        .addField("Feels Like", `**${result[0].current.feelslike}ºC**`, true)
        .addField("Status", `**${result[0].current.skytext}**`, true)
        .setTimestamp()
        .setThumbnail(result[0].current.imageUrl)
        .setColor('YELLOW')
        .setFooter(`Requested by: ${message.author.tag}`)
        message.channel.send({ embed: embed })
})};
}

谢谢

1 个答案:

答案 0 :(得分:0)

机器人无法读取 current 的属性。您在 current 之后使用了 results[0],这意味着它是未定义的。我建议登录 results 以查看其中的内容,以了解如何获取所需数据。