Skip to content
Advertisement

How to use a SQL Result in Discord.JS SelectMenus?

I am trying to add Options to a SelectMenu from a SQL request. My code is the following:

case "edit":{
                const editrow = new MessageActionRow();
                let query = `SELECT * FROM tickettypes WHERE guild_id = '${interaction.guild.id}'`
                const editmenu = new MessageSelectMenu()
                    .setCustomId('editselect')
                    .setPlaceholder('🔧 Select tickettype to edit')
                    .addOptions([
                        {
                            label: 'None',
                            description: 'Empty',
                            value: 'none',
                        },])
                await sql.query(query, function(err, result) {
                    if(err) throw err;
                    console.log(result)
                    result.forEach(item => {
                        editmenu.addOptions([
                            {
                                label: '' + item.name,
                                description: '' + item.name,
                                emoji: '' + item.emoji,
                                value: '' + item.id,
                            },
                        ]);
                    });
                });

                editmenu.addOptions([
                    {
                        label: '2 Test',
                        description: 'Empty',
                        value: 'test',
                    },
                ])
                editrow.addComponents(editmenu);
                const editembed = new MessageEmbed()
                    .setColor('YELLOW')
                    .setTitle('Edit tickettype')
                    .setAuthor({ name: interaction.user.username})
                    .setDescription('Select the tickettype you want to edit')
                await interaction.reply({ embeds: [editembed], components: [editrow], ephemeral: true})
            }
            break;

My Console is giving me the following output:

[SQL] Connected to the MySQL server! Connection ID: 95
[DISCORD] Succesfully established connection to Discord!
[
  RowDataPacket {
    id: 1,
    name: 'Test',
    emoji: '<:thumbsup:>',
    guild_id: 933323734322913300,
    prefix: 'test',
    openheader: 'Test Ticket',
    openmsg: 'Welcome, this is a test.'
  }
]

There are no Errors. Still Discord doesn’t add it. It looks like this:

Discord View of the Select Menu

I have no clue, what i did wrong. Please help me out. Thanks in advance

Advertisement

Answer

When you fetch the data from sql, your code doesn’t wait for it. It instead continues on, and the callback function you provided in the query will only be executed once the query was completed. This is why you have the option of 2 Test but you don’t have the result options. One way to fix it would be to move all the code after the result into the callback function, then they would only be executed once the query was completed

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement