【问题标题】:TypeError: Cannot read properties of undefined (reading 'list') when trying to make "urban" slash commandTypeError:尝试制作“urban”斜杠命令时无法读取未定义的属性(读取“列表”)
【发布时间】:2021-12-02 04:49:11
【问题描述】:

我的 urban-dictionary 命令与普通文本命令一样工作,但是当我尝试执行斜杠命令时 - 我的代码因TypeError: Cannot read properties of undefined (reading 'list') 错误而中断! 这是我的options 命令:

    "options": [
        {
            "name": "search",
            "description": "Term to search for",
            "type": 3,
            "required": true
        }
    ]

和我的斜杠命令代码:

let query = interaction.options.getString('search')

        query = encodeURIComponent(query);
        const { data: { list }, } = axios.get(`https://api.urbandictionary.com/v0/define?term=${query}`).catch((err) => {})
        
        const [answer] = list;

        const embed = new MessageEmbed()
        .setTitle(answer.word)
        .setURL(answer.permalink)
        .setColor("#380002")
        .setDescription(trim(answer.definition))
        .addField("Example", trim(answer.example), false)
        .addField("Upvotes", `** ** ${answer.thumbs_up}`, true)
        .addField("Downvotes", `** ** ${answer.thumbs_down}`, true)
        .setFooter(`Author: ${answer.author}`);

        interaction.reply({embeds: [embed], ephemeral: false}).catch((err) => {})

那么,你有什么想法吗?如果您需要更多信息,我会在 cmets 中回答,因为我不知道出了什么问题

【问题讨论】:

  • 相信你一定要await
  • @MrMythical 是的,它奏效了!谢谢!
  • 让我把它作为一个真正的答案发布......
  • @MrMythical 确定

标签: javascript node.js discord discord.js


【解决方案1】:

axios.get 返回一个Promise。这意味着axios.get(...).data 将是未定义的。简单的解决方法是添加await:

//async function
const { data: { list }, } = await axios.get(`...`).catch((err) => {})

【讨论】:

    猜你喜欢
    • 2022-08-04
    • 2021-12-15
    • 2022-01-05
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2021-07-29
    • 2021-10-05
    • 2018-03-07
    相关资源
    最近更新 更多