login Code:
$.ajax({
url: "/login/userlogin",
data: {
username: $("-sharpusername").val(),
userpwd: $("-sharpuserpwd").val()
},
type: "POST",
timeout: 36000,
dataType: "text",
success: function (data, textStatus) {
var dataJson = eval("(" + data + ")");
if (dataJson.lv == 1) {
alert("");
window.location.href="detail"
}
else if (dataJson.lv == 2) {
alert("lv-2");
}
else if (dataJson.code == 300) {
alert("");
}
else if (dataJson.code == 400) {
alert("");
} else {
alert("");
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("error:" + textStatus);
}
})
login.js Code:
var express = require("express");
var router = express.Router();
var mysql = require("mysql");
var config = require("../config");
var pool = mysql.createPool(config.mysql);
router.get("/", function(req, res, next) {
res.render("login");
});
router.post("/userlogin",function (req,res) {
var username = req.body.username;
var userpwd = req.body.userpwd;
console.log(username);
pool.getConnection(function (err,connection) {
var $sql = "select * from user where username=?";
connection.query($sql, [username], function (err, result) {
var resultJson = result;
if (resultJson.length === 0) {
result = {
code: 300,
msg: ""
};
res.json(result);
console.log(result);
connection.release();
}
else {
var $sql1 = "select * from user where username=?";
connection.query($sql1, [username], function (err, result) {
var temp = result[0].userpwd;
var lv = result[0].lv;
if (temp === userpwd) {
if (lv == 1)
{result = {
code: 200,
msg: "",
lv: 1};
}
else if(lv == 2)
{
result = {
code: 200,
msg: "",
lv: 2};
}
}
else {
result = {
code: 400,
msg: "",
lv: 0
};
}
res.json(result);
connection.release();
});
}
});
})
});
keep reporting errors:
C:\Users\hanhb\Desktop\cash\node_modules\mysql\lib\protocol\Parser.js:80
throw err; // Rethrow non-MySQL errors
^
TypeError: Cannot read property "query" of undefined
at C:\Users\hanhb\Desktop\cash\routes\login.js:15:20
at Handshake.onConnect [as _callback] (C:\Users\hanhb\Desktop\cash\node_modules\mysql\lib\Pool.js:58:9)
at Handshake.Sequence.end (C:\Users\hanhb\Desktop\cash\node_modules\mysql\lib\protocol\sequences\Sequence.js:88:24)
at Handshake.ErrorPacket (C:\Users\hanhb\Desktop\cash\node_modules\mysql\lib\protocol\sequences\Handshake.js:132:8)
at Protocol._parsePacket (C:\Users\hanhb\Desktop\cash\node_modules\mysql\lib\protocol\Protocol.js:279:23)
at Parser.write (C:\Users\hanhb\Desktop\cash\node_modules\mysql\lib\protocol\Parser.js:76:12)
at Protocol.write (C:\Users\hanhb\Desktop\cash\node_modules\mysql\lib\protocol\Protocol.js:39:16)
at Socket.<anonymous> (C:\Users\hanhb\Desktop\cash\node_modules\mysql\lib\Connection.js:103:28)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
Process finished with exit code 1
is it because express is not connected to mysql?