connection.query()外部的变量不会更新

时间:2019-04-11 06:21:33

标签: javascript node.js node-mysql

我有一个登录代码,该代码将数据(由用户输入)发送到mysql数据库,匹配密码并根据验证的成功或失败返回状态。为此,我定义了一个函数。状态变量在函数内部声明,并通过一些验证密码的if语句进行更新。但是,在调用该函数时,它总是返回状态的默认值

这是代码:

var con = mysql.createConnection({
 host: "localhost",
 user: "XXXXXX",
 password: "XXXXXX",
 database: "XXXXXX"
});
con.connect(function(err){
    if(err) throw err;
});

function confirm(uname,pass){
    var query = "select * from clients where name='"+uname+"' ";
    var stat = 0;
        con.query(query,function(err,result){
            if(err) throw err;
            if(result.length<1){
                stat=4;
            }   
            var password = result[0].password;
            if(password === pass){
                console.log('verified');
                stat=1;
            }
            if(password!=pass){
                stat=2;

            }
        });
    return stat;
}

问题在于,console.log('verified')在满足条件时被执行。但是,当调用此函数时,它总是返回0

如何使它更新stat变量?

2 个答案:

答案 0 :(得分:2)

查询将异步执行。因此,如果执行该函数,将在执行con.query(...)的回调之前返回stat。因此stat始终为0。

function confirm(uname, pass){
    var query = "select * from clients where name='"+uname+"' ";
    var stat = 0;
        con.query(query, function(err, result){
            // This is executed delayed. return is already called.
            if(err) throw err;
            if(result.length<1){
                stat=4;
            }   
            var password = result[0].password;
            if(password === pass){
                console.log('verified');
                stat=1;
            }
            if(password != pass){
                stat=2;

            }
        });
    return stat;
}

要解决此问题,您必须自己使用回调:

function confirm(uname, pass, cb){
    var query = "select * from clients where name='"+uname+"' ";
    var stat = 0;
        con.query(query, function(err, result){
            if(err) return cb(err);
            if(result.length < 1){
                stat=4;
            }   
            var password = result[0].password;
            if(password === pass){
                console.log('verified');
                stat=1;
            }
            if(password != pass){
                stat=2;
            } 
            cb(null, stat);
        });
}

并这样称呼它:

confirm("<username>", "<pw>", function(error, stat) {
    if (error) 
    {
          // do error routine
    } else {
         // all fine, stat is set, no error thrown
    }
}

答案 1 :(得分:-1)

那是因为您的return语句在回调之外。试试这个:

var con = mysql.createConnection({
 host: "localhost",
 user: "XXXXXX",
 password: "XXXXXX",
 database: "XXXXXX"
});
con.connect(function(err){
    if(err) throw err;
});

function confirm(uname,pass){
  return new Promise((resolve, reject) => {
    var query = "select * from clients where name='"+uname+"' ";
    var stat = 0;
    con.query(query,function(err,result){
      if(err) throw err;
      if(result.length<1){
        stat=4;
      }   
      var password = result[0].password;
      if(password === pass){
        console.log('verified');
        stat=1;
      }
      if(password!=pass){
        stat=2;
      }
        resolve(stat);
    });   
  });
}

然后,当您调用该函数时,需要使用Promise / Async语法。

Promise语法: confirm('yourusername', 'yourpassword').then(stat => {what to do with stat});

异步语法: let stat = await confirm('yourusername', 'yourpassword');