Christopher Shaw
Christopher Shaw

Reputation: 734

NodeJS Returning Data

I have the following code which isn't returning data properly.

app.post('/login',function(req,res){
    sess=req.session;
    var au = authme(req.body.name,req.body.pass, function(err,data) {
            if(err) {
                    return 'error';
            }
            console.log(data);
            return data;
    });     
    if(au) {
            sess.username = au.name;        
    }
    res.end('done');
});

Data is passed all the way to console.log(data); but when I try to use in in the au statement, its returning undefined.

Upvotes: 0

Views: 145

Answers (3)

shershen
shershen

Reputation: 9993

In Nodejs operations are asynchronous - in short it means that result is resolved later, you don't know exactly when, while code execution goes on, not waiting for it. The way to handle such call are the callbacks. More on that topic here and here.

So with your code you fall into that classical trap. This part of the code

   var au = authme(req.body.name,req.body.pass, function(err,data) {
            if(err) {
                    return 'error';
            }
            console.log(data);
            return data;
    }); 

is an asynchronous call, that means that this part

 function(err,data) {
                if(err) {
                        return 'error';
                }
                console.log(data);
                return data;
        }

is a callback that runs only when result is obtained. While this part

if(au) {
            sess.username = au.name;        
    }

is executed immediately after var au = authme(req.body.name,req.body.pass) is done. By that moment there is no au resolved so that is why you're getting this error.

In your case you should put au check into the callback:

authme(req.body.name,req.body.pass, function(err,data) {
            if(err) {
                    return 'error';
            }
            if(au) {
              req.session.username = au.name;        
        }
            res.end('done');
    }); 

Upvotes: 1

sam100rav
sam100rav

Reputation: 3783

Probably the functional call to authme is asynchronous. Put your if statement in the callback of the function call, to assure it is always validated after the asynchronous function has completed execution.

app.post('/login',function(req,res){
    sess=req.session;
    (authme(req.body.name,req.body.pass, function(err,data) {
            if(err) {
                    return 'error';
            }
            console.log(data);

            if(data) {
                    sess.username = au.name;        
            }
    }))();
    res.end('done');
});

Upvotes: 1

Pier-Luc Gendreau
Pier-Luc Gendreau

Reputation: 13814

This is the classic async problem. authme is running asynchronously so the code isn't running simply from top to bottom.

var au = authme(req.body.name,req.body.pass, function(err,data) {
  console.log('I am second!')
});     

console.log('I am first!')

You need to restructure the code a bit to get the desired behavior.

app.post('/login',function (req,res) {
  authme(req.body.name,req.body.pass, function(err, data) {
    if (err) {
      return 'error';
    }

    if (data) {
      req.session.username = data.name;
    }

    res.end('done');
  });
});

Upvotes: 2

Related Questions