简体   繁体   中英

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

My urban-dictionary command is working just fine as common text command but when I'm trying to make a slash command - my code breaks with TypeError: Cannot read properties of undefined (reading 'list') error! Here are my options for the command:

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

and my slash command code:

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) => {})

So, any ideas on what's wrong? If you need more info I will answer in comments because I have no idea what is wrong

axios.get returns a Promise . This means that axios.get(...).data will be undefined. The simple fix is to add await :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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