0

I am new to Nodejs. I am trying to return data. after select query. Here i am writing two condition's. first is very simple in logic and it is working but let me know why second condition is not working.

First Condition:

var arr =  {email:"john@gmail.com", password:"};  
return arr;

databaseConnection.query("SELECT * FROM users where email = '"+email+"' and password = '"+password +"'", function (err, result) {

 });

Second Condition

databaseConnection.query("SELECT * FROM users where email = '"+email+"' and password = '"+password +"'", function (err, result) {     

var arr =  {email:"john@gmail.com", password:"};  
return arr;

});

from passport.js

var LocalStrategy   = require('passport-local').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var TwitterStrategy = require('passport-twitter').Strategy;
var configAuth = require('./auth');


module.exports = function(passport,databaseConnection) {

    var usermodule   = require('../models/user')(databaseConnection);

passport.serializeUser(function(user, done) {
        done(null, user);
    });

passport.deserializeUser(function(user, done) {
        done(null, user);
    });

passport.use('local-login', new LocalStrategy({

        usernameField : 'username',
        passwordField : 'password',
        passReqToCallback : true // allows us to pass back the entire request to the callback

    },
    function(req, email, password, done) { // callback with email and password from our form

            var user={};


                var result = usermodule.login(email,password);

                    console.log('usercraeted');
                    user["email"]=email;
                    user["status"]=true;
                    user["name"]="John Snow";
                    user["avatar"]="";
                    user["loginStatus"]=true;
                    return done(null, user);

                user["msg"]="invalide email";
                console.log("out");


            return done(null, false,user["msg"]);
        };

    );

};

3 Answers3

1

In your first example, the code is executed up to this point:

var arr =  {email:"john@gmail.com", password:"};  
return arr;

// interpreter exits and `{email:"john@gmail.com", password:"}` is returned,
// `databaseConnection.query("` is never executed

In your second example, the code is executed up to this point:

databaseConnection.query("SELECT * FROM users where email = '"+email+"' and password = '"+password +"'", function (err, result) {     

var arr =  {email:"john@gmail.com", password:"};  
return arr;

});
// interpreter exits, undefined is returned
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
0

The second condition doesn't work because node.js is asynchronous. And your return is on a callback that take some times to be executed.

boubaks
  • 352
  • 2
  • 8
  • so how to execute my code ? –  Dec 07 '16 at 20:41
  • What do you exactly want to do ? – boubaks Dec 07 '16 at 20:53
  • You can directly use function inside your callback – boubaks Dec 07 '16 at 20:54
  • I am calling this function from other page. it returns undefined... i want to get results from database and return results... results exists in array... as i found in console... but returned results are undefined on other page where i am calling function –  Dec 07 '16 at 21:02
0

You have to use callbacks.

function getUser() {
   var email = "test@test.com";
   var password = "test";
   getDatas(email, password, function(err, result) {
       // the result is good and not undefined if no errors :)
   });
}

function getDatas(email, password, callback) {

   databaseConnection.query("SELECT * FROM users where email = '"+email+"' and password = '"+password +"'", function (err, result) {     
       callback(err, result); // callback is executed with the result you
   });
}

Hope I helped you

boubaks
  • 352
  • 2
  • 8