3Lectric_
3Lectric_

Reputation: 13

Return value for a function is undefined

I have been stuck on this for a while and I don't know what's wrong. I have a separate class called Databse.js that runs the MySQL functions, it manages the connection, queries, etc. I have been trying to get it to grab information form the 1 row it should return(A user with the correct username and password) however it says the variable is undefined. If I log the objects fields in the function before I return it, they all work fine.

Here is the code with a little more context:

socket.on('loginAttempt', function(data) {
    //Check credentials with database...
    try {
        var fuser = Database.checkAccount(data.username, data.password);
        if (fuser === undefined) {//This is always being called
            console.log("fuser is undefined");//For debug only
        }
    } catch(err) {
        console.log(err.message);
    }
    try {
        if (fuser !== undefined) {
            socket.emit('loginMessage', {status:true});

            socket.id = Math.random();
            SOCKET_LIST[socket.id] = socket;

            var player = Player(socket.id, fuser.Username, fuser.Rank, fuser.Points);
            PLAYER_LIST[socket.id] = player;
            return;
        }
        socket.emit('loginMessage', {status:false});
    } catch(err) {
        console.log(err.message);
    }
});

That is the function that calls the troubled object, here is the checkAccount() function from Databse.js(I've included my module.exports if that helps):

function checkAccount(username, password) {
con.query("SELECT * FROM Userbase WHERE Username='" + username + "' AND Password='" + password + "';", function(err, rows) {
    if (err) throw err;
    if (rows.length > 0) {
        var returnedUser = new User();
        returnedUser.Username = username; 
        returnedUser.Password = password; 
        returnedUser.Rank = rows[0].Rank;
        returnedUser.Points = rows[0].Points;
        return returnedUser; // This should return the User object however when run in my app.js it is undefined
    }
    return null;
});

}

module.exports.mysql = mysql;
module.exports.con = con;
module.exports.checkAccount = checkAccount;
module.exports.User = User;
module.exports.init = init;

Any help would be appreciated! c:

Upvotes: 1

Views: 86

Answers (1)

Dimitri Lavrenük
Dimitri Lavrenük

Reputation: 4879

your function checkAccount(username, password) { has no return value.

only the function you pass to query(query, callback) returns a value. Since the query function is asyncronous it should return a promise or call a callback when the call is ready. You should read more about callbacks and promises f.e. here: http://sporto.github.io/blog/2012/12/09/callbacks-listeners-promises/

Depending on what database implementation you use it is possibly that the query function returns a promise, than you could do this:

function checkAccount(username, password) {
    return con.query(...

and use the promise in that way

checkAccount(xxx).then(function(data) {
    // your data is here
});

Upvotes: 1

Related Questions