我使用mysql和express来编写一个简单的登录系统。
database.js:
var mysql = require('mysql');
var config = require('../config/config');
var pool = mysql.createPool(config.mysql_dev);
var query=function(sql,callback){
pool.getConnection(function(err,conn){
if(err) console.log("POOL ==> " + err);
else{
conn.query(sql,function(qerr,vals,fields){
//release connection
conn.release();
callback(qerr,vals,fields);
});
}
});
};
module.exports=query;
index.js:
router.post('/login',function(req,res){
var query = require('../modules/database');
query("select * from managers where ManagerID =10001",function(err,vals,fields){
var temp=JSON.stringify(vals);
var manager = JSON.parse(temp)[0];
if(req.body.password===manager.password){
req.session.manager = manager;
res.redirect('/home');
}
res.send('ID or password wrong!');
});
});
但是,每当我收到此错误时:
/opt/workspace/project/nodejs-demo/node_modules/mysql/lib/protocol/Parser.js:77
throw err; // Rethrow non-MySQL errors
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.header (/opt/workspace/project/nodejs-demo/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/opt/workspace/project/nodejs-demo/node_modules/express/lib/response.js:163:12)
at /opt/workspace/project/nodejs-demo/routes/index.js:40:7
at Query._callback (/opt/workspace/project/nodejs-demo/modules/database.js:16:17)
at Query.Sequence.end (/opt/workspace/project/nodejs-demo/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
at Query._handleFinalResultPacket (/opt/workspace/project/nodejs-demo/node_modules/mysql/lib/protocol/sequences/Query.js:144:8)
at Query.EofPacket (/opt/workspace/project/nodejs-demo/node_modules/mysql/lib/protocol/sequences/Query.js:128:8)
at Protocol._parsePacket (/opt/workspace/project/nodejs-demo/node_modules/mysql/lib/protocol/Protocol.js:280:23)
at Parser.write (/opt/workspace/project/nodejs-demo/node_modules/mysql/lib/protocol/Parser.js:73:12)
如何解决? 我不知道如何解决它并搜索了很长时间。 如果我不清楚地描述它,只需评论,我将添加我知道的任何内容。
答案 0 :(得分:1)
我在代码中的注释中解释了您的问题。
router.post('/login',function(req,res){
var query = require('../modules/database');
query("select * from managers where ManagerID =10001",function(err,vals,fields){
var temp=JSON.stringify(vals);
var manager = JSON.parse(temp)[0];
if(req.body.password===manager.password){
req.session.manager = manager;
//You are redirecting user to home
res.redirect('/home');
//You should add 'return'
return;
}
//This will only accessible if password is not match
res.send('ID or password wrong!');
});
});
答案 1 :(得分:0)
我应该回到if:
if(req.body.password===manager.password)
{
req.session.manager = manager;
res.redirect('/home');
return;
}
答案 2 :(得分:0)
您在以下代码段中遇到问题。
if(req.body.password===manager.password){
req.session.manager = manager;
res.redirect('/home');
}
res.send('ID or password wrong!');
从错误堆栈跟踪中可以清楚地看到,您尝试在发送标头后设置标头。这意味着if
语句首先执行,如果是,则重定向到/home
,然后退出if
子句并尝试向用户发送消息。
可能的解决方案
if(req.body.password===manager.password){
req.session.manager = manager;
res.redirect('/home');
} else {
res.send('ID or password wrong!');
}
检查它,它是否对你有帮助。