execute function not ending after return statement?

Hello, I have the following assign.js command that I am trying to use to assign a targeted user to a specific team, which is kept track of in a MySQL database. https://hastebin.skyra.pw/tusanaxana.js When the user is not found to have registered in the database, I am expecting that the function should stop, because there is a return statement on line 26. However, this is not the case. All the subsequent lines of code still continue to run, and I'm not sure why. Any help would be greatly appreciated, thank you! This is what I get logged in my console after I run the command and purposefully select a user that has not registered, along with a team name that I know is not valid. https://hastebin.skyra.pw/egaxekiweb.txt
7 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers.
Love
Love11mo ago
discord.js@14.11.0 and node v18.15.0 I have a feeling that the issue has something to do with the return statement on line 26 being nested within the callback function of con.execute() on line 22, but I’m not sure how to properly solve the problem
Fisban Regner
Fisban Regner11mo ago
The return just stops the function and returns to your code. Why the interaction.reply isn't working unsure What I would do is have the function return a state then test for the state if it passes then continue the code if it fails end execution and send your reply
if (!con.execute(`SELECT discord_id FROM users WHERE discord_id = ?;`, [target.id], (err, result) => {
if (err) throw err;
// If they have not registered, let the author know and stop running.
if (result.length === 0) {
return false
}
});) {interaction.reply({ content: `something went wrong. ${target} has not yet registered for the event.`, ephemeral: true });}
else {the rest of your code}
if (!con.execute(`SELECT discord_id FROM users WHERE discord_id = ?;`, [target.id], (err, result) => {
if (err) throw err;
// If they have not registered, let the author know and stop running.
if (result.length === 0) {
return false
}
});) {interaction.reply({ content: `something went wrong. ${target} has not yet registered for the event.`, ephemeral: true });}
else {the rest of your code}
some thing like this This was sent feom my phone so it may contain errors it is to be taken as an example and not an actual solution
Love
Love11mo ago
so if I had something like
const something = con.execute(`SELECT discord_id FROM users WHERE discord_id = ?;`, [target.id], (err, result) => {
if (err) throw err;
// If they have not registered, let the author know and stop running.
if (result.length === 0) {
return true;
}
});
const something = con.execute(`SELECT discord_id FROM users WHERE discord_id = ?;`, [target.id], (err, result) => {
if (err) throw err;
// If they have not registered, let the author know and stop running.
if (result.length === 0) {
return true;
}
});
then my something variable would hold the value true?
Fisban Regner
Fisban Regner11mo ago
It's should but you may want to do an await con.execute so that something has the intended value
Love
Love11mo ago
Thanks! I will give this a shot when I get home
Fisban Regner
Fisban Regner11mo ago
Just think of it this way
your code
Your function { code
Return // end function return to code }
More code after function
your code
Your function { code
Return // end function return to code }
More code after function
if you want your code to stop after the function you will also need to tell the code to stop. If the function will take more than milliseconds to complete ie connecting to an outside source you will need to await the promise or you will recieve un intended results