Nodejs,返回条件不起作用

时间:2016-12-07 19:20:46

标签: node.js

我是Nodejs的新手。我正在尝试返回数据。选择查询后。我在这里写两个条件。首先是逻辑非常简单,它正在工作,但让我知道为什么第二个条件不起作用。

第一个条件:

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

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

 });

第二个条件

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

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

});

来自 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 个答案:

答案 0 :(得分:1)

在第一个示例中,代码执行到此时为止:

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

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

在第二个示例中,代码执行到此为止:

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

答案 1 :(得分:0)

第二个条件不起作用,因为node.js是异步的。而且你的回程是在一个需要执行一段时间的回调上。

答案 2 :(得分:0)

你必须使用回调。

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
   });
}

希望我帮助你