我正在使用带有快速框架的nodejs而我的db是postgres db.I有2个问题。第一,当我使用select查询执行登录验证时它正确执行假设用户不在db中查询终止如何我可以使用if else在这里没有使用end.all我在其他部分查询块。然后第二,我使用2次res.send所以我得到一个错误我怎么能渲染页面并发送数据到ajax电话。
script.
$(document).ready
console.log('bbbbbbbb');
$('#loginform').submit(function(e) {
console.log('submit called');
var data = {};
data.email = $('#username').val();
data.password = $('#password').val();
JSON.stringify(data);
console.log(data);
$.ajax({
url: "/login",
type: "POST",
dataType: 'json',
data: JSON.stringify({ "objectData": data}),
contentType: "application/json",
success: function(data) {
alert('SUCCESS":'+JSON.stringify(data));
var successVar = data;
alert(successVar.data);
if(successVar.data == "Invalid username And Password"){
location.href='/';
}
if(successVar.data == "Login successfully"){
location.href='http://localhost:3000/landing';
}
},
});
return false;
});
My call
exports.login = function(req, res) {
var resultObject = (req.body.objectData);
var user = client.query("SELECT * FROM login where email_id='" + resultObject.email + "' AND password='" + resultObject.password + "'");
user.on("row", function(row ,err) {
console.log(row);
var register_id = row.login_id;
console.log(register_id);
res.send({data:"Login successfully"});
res.render('landing.jade');
});
user.on("end", function(result) {
res.send({data:"Invalid username And Password"});
});
};
答案 0 :(得分:1)
享受此代码
exports.login = function(req, res) {
console.log("enter the login999999999");
console.log(req.body);
var resultObject = (req.body.objectData);
client.query("SELECT * FROM login where email_id='" + resultObject.email + "' AND password='" + resultObject.password + "'", function(err, result) {
console.log("Row count: %d",result.rows.length); // n
if(result.rows.length>0){
console.log('successfully');
res.send({data:"Login successfully"});
} else {
console.log('not successfully');
res.send({data:"Invalid username And Password"});
}
});
};
答案 1 :(得分:0)
很简单:
res.render('landing',{'data':data})