0

I'm new to node and I tried to make a basic app with authentification . Data are stored on a mongoDB remote server. My HTML form POST data to my server URL. Here the route :

app.post('/auth', function(req, res){
handleRequest(req, res);

});

And the called handler :

function handleRequest(request, response) {
    if (request.method == 'POST') {
        console.log("Trying to get POST");
        var body = '';
        request.on('data', function (data) {
            body += data;
        });

        // Get datas, parse them and create user with it
        request.on('end', function () {
            var data = JSON.parse(body);
            var login = data.login;
            var password = data.password;
            var email = data.email;

            myUser = userClass.create(login,email,password);

            console.log ("email : "+email);
            console.log ("password : "+password);


            // authenticate with user
            var auth = userClass.authenticate(myUser,function(result){
                console.log("result = "+result);
                    });

             });
    }
}

The userClass.authenticate :

exports.authenticate = function(user,callback){
    var result = "false";
    var query = User.where(
    { 
        email : user.email,
        password : user.password

    });
    query.findOne(function(err,user){
        if(err){return handleError(err);}
        if(user){
            result = "true";
        }
        console.log(user);
    });
    console.log("callback inc")
    callback(result);
}

I'm pretty sure it's not optimized but it's not what I'm looking for. When I launch the server and I send it some POST (correct) data, this strange thing happens : execution

My user stored in remote DB is found , so in userClass.authenticate result = true But when the callback function is ran, the log say it's false. Did I do a something wrong in the callback ?

0

1 Answer 1

1

if query.findOne is Asynchronous, you're calling the callback before findOne is complete. Put the callback(result) inside the findOne callback - like this

exports.authenticate = function(user,callback){
    var result = "false";
    var query = User.where(
    { 
        email : user.email,
        password : user.password

    });
    query.findOne(function(err,user){
        if(err){return handleError(err);}
        if(user){
            result = "true";
        }
        console.log(user);
        callback(result);
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's a point I didn't saw before. Callback calls must been done inside the calling function ? Thx for fast response, I mark as solved in 2 mins
Callback calls must been done inside the calling function in this case, yes, because findOne is asynchronous, you wont have a value until the findOne callback is called, so it's in this callback you call your callback

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.