express无法连接到mysql数据库
login代码:
$.ajax({
url: "/login/userlogin",
data: {
username: $("#username").val(),
userpwd: $("#userpwd").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代码:
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();
});
}
});
})
});
一直报错:
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
是因为express没有连接到mysql吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
pool.getConnection 你这个方法是什么东西? connection.query 先log一下connection是什么东西,connection没有query这个方法呢
你的mysql配置有问题,检查一下你var config = require('../config');这里的的配置
324234234