generic-pool操作mysql的问题: 如何取出最终的结果?
这是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;
});
});
感恩啊!!! 不过还是有点小问题:
$.post("/login", {loginUser: regName, loginPwd: regPwd}, function(data){
alert(data);
});
这点是不是用到了叫做 "ajax’'的东西? 有了这个html中的form就不是必须的了吧?
@sureleo 恩,可以不要了,单我们一般还是写上了!
@javanode 谢谢你的帮助! “记录在nodejs学习过程中踩过的那些坑” 有时间一定拜读!