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