2016-12-07 79 views
0

我是Nodejs的新手。我正在嘗試返回數據。選擇查詢後。在這裏,我寫了兩個條件。首先是非常簡單的邏輯,它正在工作,但讓我知道爲什麼第二個條件不起作用。Nodejs,返回條件不起作用

第一個條件:

var arr = {email:"[email protected]", 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:"[email protected]", 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"]); 
     }; 

    ); 

}; 

回答

2

在你的第一個例子中,代碼被執行了這一點:

var arr = {email:"[email protected]", password:"}; 
return arr; 

// interpreter exits and `{email:"[email protected]", 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:"[email protected]", password:"}; 
return arr; 

}); 
// interpreter exits, undefined is returned 
+0

如何從數據庫返回結果? – SDA

+0

@SyedDaniyalAsif,這段代碼在哪裏運行?你想在哪裏返回結果?發佈完整的代碼。 –

+0

請檢查它 – SDA

0

在S第二個條件不起作用,因爲node.js是異步的。而你的回報是需要一段時間才能執行的回調。

+0

所以如何執行我的代碼? – SDA

+0

你到底想做什麼? – boubaks

+0

您可以直接在回調中使用函數 – boubaks

0

你必須使用回調。

function getUser() { 
    var email = "[email protected]"; 
    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 
    }); 
} 

希望我幫你