Function does not give result in NodeJS

I try to call a function i made to test if a username exist or not in the database.

The function work but when i try to call the function in the console to see the return), i always get undefined.

Here is my test function:

function testUsername(pseudo, uid) {

    User.findOne({username:pseudo}).exec(function (err, data) {

        if (err) {
            return 'Error fetching user database';
        }
        else if((data == null || data == '') || data._id == uid)
        {
            return 'ok';
        }
        else
        {
            return 'error';
        }

    });
}

Here is the call :

var check = testUsername('blabla', 123456);
console.log(check);


Source: javascript

Leave a Reply