这是gamerService中的一段代码, 然后我想在routes中调用这个代码,
exports.authenticateGamer = function(username, password){
q.pool.acquire(function(err,client){
if(err){}
else{
client.query("SELECT * FROM gamer WHERE username = ?, password = ? "
,[username,password], function(err,data){
q.pool.release(client);
});
}
});
}
app.post("/login",routes.login)代码如下:
exports.login = function(req,res){
var gamerName = req.body.loginUser;
var password = req.body.loginPwd;
var temp = gamerService.authenticateGamer(gamerName,password);
console.log(temp);
}
但是我肯定我的输入是没有错的, 而temp却始终不能得观察家我想要的结果而一直是undefined.
问题延伸: 我如何将exports.login得到的值返回到前台的js上, 当temp是空的时候让前台的js弹出一个alert? 并附前台js代码:
$(document).ready(function(){
$('#registerBtn').click(function(){
if(!$('#reg-name').val()){
alert("Username can't be empty!");
return false;
}
if($('#reg-pwd').val()!=$('#reg-cpwd').val()||!$('#reg-pwd').val()){
alert("Passwords are different or empty!");
return false;
};
});
});
感恩!
4 回复
exports.authenticateGamer = function (username, password, callback) {
q.pool.acquire(function (err, client) {
if (err) {
}
else {
client.query("SELECT * FROM gamer WHERE username = ?, password = ? "
, [username, password], function (err, data) {
if(err) throw err
callback(data);
q.pool.release(client);
});
}
});
}
app.post("/login", routes.login);
exports.login = function (req, res) {
var gamerName = req.body.loginUser;
var password = req.body.loginPwd;
gamerService.authenticateGamer(gamerName, password, function(data){
console.log(data);
res.json(data);
});
}
$(document).ready(function(){
$('#registerBtn').click(function(){
var regName = $('#reg-name').val();
var regPwd = $('#reg-pwd').val();
var regCpwd = $('#reg-cpwd').val();
if(!regName){
alert("Username can't be empty!");
return;
}
if(regPwd != regCpwd || !regCpwd){
alert("Passwords are different or empty!");
return;
}
$.post("/login", {loginUser: regName, loginPwd: regPwd}, function(data){
alert(data);
});
return false;
});
});