繁体   English   中英

类型错误:尝试执行“城市”斜线命令时无法读取未定义的属性(读取“列表”)

[英]TypeError: Cannot read properties of undefined (reading 'list') when trying to make "urban" slash command

我的城市词典命令与普通文本命令一样正常工作,但是当我尝试创建斜杠命令时 - 我的代码因TypeError: Cannot read properties of undefined (reading 'list')中断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) => {})

那么,关于什么是错的任何想法? 如果您需要更多信息,我会在评论中回答,因为我不知道出了什么问题

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM