我有2个js文件,一个用于客户端,一个用于服务器。客户端输入登录数据,并将其发送到服务器。服务器检查是否找到了用户电子邮件,然后检查密码是否正确。我正在执行一条if语句来验证是否填充了“结果”,然后继续检查密码是否正确。但是,我继续遇到此错误node.js error,并且在浏览器中显示了“”。你能帮我么?我已经被困了好几天了。
Client.js
var textbox;
var emailtextbox;
var dataDiv;
window.onload = initLogin;
function initLogin(){
textbox = document.createElement("input");
textbox.id="textbox";
emailtextbox = document.createElement("input");
emailtextbox.id="emailtextbox";
dataDiv = document.createElement("div");
var header = document.createElement("h1");
header.appendChild(document.createTextNode("Select User"));
var button = document.createElement("BUTTON");
button.id = "myBtn";
var textBtn = document.createTextNode("Click me");
button.appendChild(textBtn);
button.addEventListener("click", () => {
SendLoginData();
});
var docBody = document.getElementsByTagName("body")[0];//Only one body
docBody.appendChild(header);
docBody.appendChild(dataDiv);
docBody.appendChild(textbox);
docBody.appendChild(emailtextbox);
docBody.appendChild(button);
}
function SendLoginData(){
var email = document.getElementById("emailtextbox").value; //I want to send it to server.js
var password = document.getElementById("textbox").value;
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var dataObj = JSON.stringify(this.responseText);
dataDiv.innerHTML = dataObj;
}
};
xhttp.open("GET", "/login/" +password+ "&"+email, true);
xhttp.send();
}
Server.js
var express = require('express');
var app = express();
app.get('/login/*', handleGetRequest); //how do I pass usrName here?
app.use(express.static('public'));
app.listen(5000);
function handleGetRequest(request, response){
var pathArray = request.url.split("/");
var processID = pathArray[pathArray.length - 2];
var pathEnd = pathArray[pathArray.length - 1];
if(processID === 'register'){
response.send("{working}");
}
else
var registerArray = pathEnd.split("&");
searchEmail(registerArray,response);
}
function searchEmail(registerArray, response){
var mysql = require('mysql');
var con = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'customer',
port: 6000
});
con.connect();
var answer = selectData();
response.send(answer);
//Function that adds test data to database
function selectData(){
var sql = "SELECT * FROM cust where LastName = '" +registerArray[0]+"'";
con.query(sql, function (err, result) {
console.log(JSON.stringify(result)); //correctly displaying stuffs from db
if (JSON.parse(result).length) { //statement not working correctly.
var pw = "SELECT ID FROM cust where LastName = '" +registerArray[0]+"'";
con.query(pw, function (err, result) {
var idcheck = JSON.parse(result);
console.log(result); //always shows undefined
if (idcheck !== registerArray[1]){
return ("{re enter correct password}");
} else {
return ("{login successfully}");
}
});
} else {
return("{sign up first}");
}
});
}
con.end();
答案 0 :(得分:0)
尝试这样:
var response = JSON.parse(JSON.stringify(result[0]));
if (response.LastName) {
var pw = "SELECT ID FROM cust where LastName = '" +registerArray[0]+"'";
con.query(pw, function (err, result) {
var idcheck = JSON.parse(result);
console.log(result); //always shows undefined
if (idcheck !== registerArray[1]){
return ("{re enter correct password}");
} else {
return ("{login successfully}");
}
});
} else {
return("{sign up first}");
}
如果您用console.log记录查询结果,如下所示:
console.log(result[0]);
您将获得RowDataPacket对象:
RowDataPacket { firstName: "My First Name", lastName: "My Last Name" }
这就是为什么您需要首先对其进行字符串化,使其变得像这样:
{ "firstName": "My First Name", "lastName": "My Last Name" }
最后,您可以解析它,并通过验证LastName属性是否存在来检查是否找回了用户。因此,全部在一行:
var response = JSON.parse(JSON.stringify(result[0]));