await not behaving nicely after displaying a modal

Very silly question relating to async/await. After I display a modal, I collect the input using:
var submittedModal = await interaction.awaitModalSubmit({ time: 60000 });
if(submittedModal) {
// process here
}
var submittedModal = await interaction.awaitModalSubmit({ time: 60000 });
if(submittedModal) {
// process here
}
In the if(submittedModal) {} block, my await commands no longer seem to work asynchronously. For example, I'm trying to make a call to a MySQL database based on the result of the modal (await connection.promise().query('select * ...), but it doesn't wait for the results of that query before barrelling on. Am I doing something wrong?
13 Replies
d.js toolkit
d.js toolkit16mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
ShompiFlen
ShompiFlen16mo ago
show your code for the db query but like with more context
Alli
AlliOP16mo ago
sure! 1 sec
await interaction.showModal(modal);
var submittedModal = await interaction.awaitModalSubmit({ time: 60000 });
if (submittedModal) {
var cwflag_name = submittedModal.fields.getTextInputValue('flagName');
var value = submittedModal.fields.getTextInputValue('flagValue');
if (visibility == 'cflag') { //visibility is set elsewhere in user input, i verified this is working
var cwflags = await connection.promise().query('select * from characterflags where lower(name) like lower("%?%") and guild_id = ?', [cwflag_name, interaction.guildId]); //interaction.guildId is set properly, I'm using that from the slash command that triggered the modal in the first place
console.log(cwflags);
} else {
var cwflags = await connection.promise().query('select * from worldflags where lower(name) like lower("%?%") and guild_id = ?', [cwflag_name, interaction.guildId]);
console.log(cwflags);
}
await submittedModal.reply({ content: 'Checking for flags...', ephemeral: true });
if (cwflags[0].length < 1) {
await submittedModal.editReply({ content: "No flags with that name exist.", ephemeral: true });
} else if (cwflags[0].length == 1) {
await connection.promise().query('insert into reputations (name, guild_id, description, visibility, maximum, start_value, cwflag_id, cwflag_value) values (?, ?, ?, ?, ?, ?, ?, ?)', [name, interaction.guildId, description, visibility, maximum, start_value, cwflags[0][0].id, value]);
await submittedModal.editReply({ content: "Reputation added.", ephemeral: true });
}
await interaction.showModal(modal);
var submittedModal = await interaction.awaitModalSubmit({ time: 60000 });
if (submittedModal) {
var cwflag_name = submittedModal.fields.getTextInputValue('flagName');
var value = submittedModal.fields.getTextInputValue('flagValue');
if (visibility == 'cflag') { //visibility is set elsewhere in user input, i verified this is working
var cwflags = await connection.promise().query('select * from characterflags where lower(name) like lower("%?%") and guild_id = ?', [cwflag_name, interaction.guildId]); //interaction.guildId is set properly, I'm using that from the slash command that triggered the modal in the first place
console.log(cwflags);
} else {
var cwflags = await connection.promise().query('select * from worldflags where lower(name) like lower("%?%") and guild_id = ?', [cwflag_name, interaction.guildId]);
console.log(cwflags);
}
await submittedModal.reply({ content: 'Checking for flags...', ephemeral: true });
if (cwflags[0].length < 1) {
await submittedModal.editReply({ content: "No flags with that name exist.", ephemeral: true });
} else if (cwflags[0].length == 1) {
await connection.promise().query('insert into reputations (name, guild_id, description, visibility, maximum, start_value, cwflag_id, cwflag_value) values (?, ?, ?, ?, ?, ?, ?, ?)', [name, interaction.guildId, description, visibility, maximum, start_value, cwflags[0][0].id, value]);
await submittedModal.editReply({ content: "Reputation added.", ephemeral: true });
}
ShompiFlen
ShompiFlen16mo ago
first of all stop using var specially if you are declaring variables with the same name, because they basically get merged, use let or const, var is already out of spec basically
Alli
AlliOP16mo ago
'kay!
ShompiFlen
ShompiFlen16mo ago
so you say that cwflags is coming as undefined?
Alli
AlliOP16mo ago
Right, there's nothing there when I log it
ShompiFlen
ShompiFlen16mo ago
what module are you using for this db calls
Alli
AlliOP16mo ago
mysql2 looked further, i'm going to close this as not discordjs related, i'm doing this with an insert in another bot i wrote and it behaves perfectly fine there - i just matched all the syntax and it looks exactly correct, so this appears to be out of scope thank you though ❤️
ShompiFlen
ShompiFlen16mo ago
alright i was gonna say why you were calling promise before the query you couldve use execute aswell
Alli
AlliOP16mo ago
i'm just very used to using connection.promise().query() for mysql2, is the short answer
ShompiFlen
ShompiFlen16mo ago
thats alright, you can close the ticket now. If you have more issues feel free to post a new one in #other-js-ts
Alli
AlliOP16mo ago
thanks! alliheart
Want results from more Discord servers?
Add your server