0

im writing a discord bot to check points from a database then return them. This is my code at the moment.

function userCheck(id, name) {
        date = new Date().toISOString().slice(0, 19).replace('T', ' ');
        con.query("SELECT id FROM users WHERE id = "+id, function (err, row) {
            if (!(row && row.length) ) {
                    con.query("INSERT INTO `users` (id,name,rank,points,signup) VALUES ('"+id+"', "+name+", '0', '0' , '"+date+"')"), function (err, result, fields){
                        if (err) throw err;
                    }
            }else{
                //fine
            }
    });
    }


    function checkPoints(user){
        id = user.id;
        name = con.escape(user.username);

        userCheck(id, name);
        console.log("SELECT points FROM users WHERE id = "+id);
        con.query("SELECT points FROM users WHERE id = "+id, function (err, result, fields){
            return result[0].points;
        });
    }

My code which calls these functions is this: return message.author.send(checkPoints(message.author));

This makes discordjs error as it is trying to return an empty message. This means my functions arent returning right. Ive look at this over and over its probably just a simple fix but I cant see it.

Thanks in advance

7
  • function checkPoints doesn't have a return statement, therefore returns undefined Commented Oct 29, 2018 at 11:31
  • Not familiar with the API, but I'm going to take a guess that con.query is an asynchronous function and you're returning in a callback function. The function ends before your results will ever come in. You need to use a promise or async/await. Commented Oct 29, 2018 at 11:32
  • or a callback will do Commented Oct 29, 2018 at 11:34
  • @Bravo it does return? after the query. It also checks before the query if the user is in the database. Commented Oct 29, 2018 at 11:35
  • no, it does not ... that return is in the function (err, result, fields){ function ... return doesn't return from all enclosing functions Commented Oct 29, 2018 at 11:36

1 Answer 1

2

function checkPoints(user){ doesn't return anything i.e. same as return undefined

Since con.query is asynchronous - the simplest fix is to use a callback, like so

function checkPoints(user, cb){
    id = user.id;
    name = con.escape(user.username);

    userCheck(id, name);
    console.log("SELECT points FROM users WHERE id = "+id);
    con.query("SELECT points FROM users WHERE id = "+id, function (err, result, fields){
        cb(result[0].points);
    });
}

checkPoints(message.author, function(result) {
    message.author.send(result);
});

or, use Promise

function checkPoints(user){
    return new Promise(function(resolve, reject) {
        id = user.id;
        name = con.escape(user.username);

        userCheck(id, name);
        console.log("SELECT points FROM users WHERE id = "+id);
        con.query("SELECT points FROM users WHERE id = "+id, function (err, result, fields){
            if(err) return reject(err);
            resolve(result[0].points);
        });
    });
}

checkPoints(message.author)
.then(function(result) {
    message.author.send(result);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.