我需要帮助解决我遇到的这个问题。如果用户名或密码不在数据库中,我有登录脚本会崩溃节点,这里是代码:
/* Login */
exports.login = function(req, res){
var post = req.body;
var mysql = require('mysql');
var db = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
database : 'node01',
});
var selectStmt = 'SELECT * FROM users WHERE name = "' + post.user +'"';
db.connect();
db.query(selectStmt, function(err, rows, fields) {
if(err) throw err;
else if(rows[0].name == post.user && rows[0].password == post.password){
req.session.auth_id = rows[0].id;
req.session.user = rows[0].name;
res.redirect('/adminindex');
}
else res.redirect('/admin');
});
db.end();
};
和我得到的错误
TypeError:无法
在 Query.Sequence.end
(../node- project01/node_modules/mysql/lib/protocol/sequences/Sequence.js:66:24)
在 Query._handleFinalResultPacket (../node-project01/node_modules/mysql/lib/protocol/sequences/Query.js:138:8)
在 Query.EofPacket (../node-project01/node_modules/mysql/lib/protocol/sequences/Query.js:122:8)
在 Protocol._parsePacket (../node-project01/node_modules/mysql/lib/protocol/ Protocol.js:165:24)
在 Parser.write (../node-project01/node_modules/mysql/lib/protocol/Parser.js:60:12)
在 Protocol.write (../node-project01/node_modules/ mysql/lib/protocol/Protocol.js:32:16)
在 Socket.ondata (stream.js:38:26)
在 Socket.EventEmitter.emit (events.js:96:17)
在 TCP.onread (net.js:396:14)
有想法该怎么解决这个吗?