1
    router.post('/loginv', function (req,res) {
  var id = req.body.id;
  var pass = req.body.pass;

  if(login.login(id,pass)=='validated'){
    res.sendfile('views/welcome.html');
  }else{
    res.send('dont give up');
  }

  var result = login.login(id,pass);
  console.log(result);
});

module.exports={


   login : function(id,pass){
        var q = "SELECT * FROM user where id = ? and pass = ?";
        var ret = 'default';

        DB.DB.query(q, [id,pass], function (error,result) {
            if(error){
                console.log('not found');
                ret = 'unrecognized';
            } else{
                console.log('found');
                ret =  'validated';
            }
        });

        return ret;
    }};

console.log :

    GET /login 304 4.028 ms - -
default
POST /loginv 200 40.558 ms - 12
found
found

as you can see the value ret returned from the following code is not being changed although it follows the procedure of the function properly.. i'm new to node js and js stuff so any comments and advice will definitely be helpful thx :)

rpadovani
  • 7,101
  • 2
  • 31
  • 50
senaflix
  • 13
  • 3
  • `DB.query()` is async, so `login` function do not wait the execution of the code before returning `ret`. You need to add a callback to the login method (or use a promise) – rpadovani Apr 27 '16 at 07:03
  • 2
    In other words, login functions returns before the complete execution of the query. Querying database is an I/O operation which is asynchronous or say non-blocking. – Luke P. Issac Apr 27 '16 at 07:30

1 Answers1

1

DB.query() is async, so login function do not wait the execution of the code before returning ret. You need to add a callback to the login method (or use a promise).

A working code:

module.exports = {
  login : function(id,pass,cb){
      var q = "SELECT * FROM user where id = ? and pass = ?";

      DB.DB.query(q, [id,pass], function (error,result) {
          if(error){
              console.log('not found');
              cb(error, 'unrecognized');
          } else{
              console.log('found');
              cb(null, 'validated');
          }
      });
  }
};

The other file:

router.post('/loginv', function (req,res) {
  var id = req.body.id;
  var pass = req.body.pass;

  login.login(id,pass, function(err, result) {
    if (err) {
        console.log(err);
        res.send('dont give up');
        return;
    }

    if (result === 'validated') {
      res.sendfile('views/welcome.html');
    }else{
      console.log('Unknown error');
    }
  })
});

I suggest you to read links posted here and this question which can gives you an idea about callbacks and promises.

PS: I do not know which library you are using for DB, but you MUST sanitize input before doing queries, if the library does not do it for you

rpadovani
  • 7,101
  • 2
  • 31
  • 50
  • it works!! thanks for your help and the links really helpful.. been trying to solve this problem for hours – senaflix Apr 27 '16 at 10:27