Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
JavaScript function returning undefined but both cases are defined

My function returns undefinded but the return value is defined.

function createcode() {
            var clientid = interaction.user.id;
            var sqlsearch = `SELECT COUNT(*) AS count FROM codes WHERE ClientID = '${clientid}'`
            database.query(sqlsearch, code, function(err, rows)  {
                if (err) throw err;
                var count = rows[0].count;
                if(count != 0)
                {
                    var exsitingcode;
                    var getexsitingcode = `SELECT codes, codes AS ecode FROM codes WHERE ClientID = '${clientid}'`;
                    database.query(getexsitingcode, async function(err, rows)  {
                        if (err) throw err;
                        exsitingcode = await rows[0].ecode;
                        console.log(`${interaction.user.username} tried creating a code but already has one: ${exsitingcode}`)
                        return exsitingcode;
                    })
                }
                else
                {
                    var code = Math.random().toString(36).slice(3);
                    var sql = `INSERT INTO codes(codes, ClientID) VALUES ('${code}', '${clientid}')`
                    database.query(sql, function(err)  {
                        if (err) throw err;
                        console.log(`Inserted Code: ${code}`)
                        setTimeout(() => {
                            var sqldelete = `DELETE FROM codes WHERE codes = '${code}'`
                            database.query(sqldelete, function (err) {
                                if (err) throw err;
                                console.log(`Code deleted: ${code}`);
                            })
                        }, 300000); // 5Min
                        console.log("returning code...")
                        return code;
                    })
                }
            })
        }

In both cases it returns undefinded. How can I bypass this?

using mysql & discord.js v13

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!