Home > OS >  TypeError: Cannot read properties of undefined (reading 'list') when trying to make "
TypeError: Cannot read properties of undefined (reading 'list') when trying to make "

Time:10-14

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

CodePudding user response:

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