nodejs连接mysql错误

我使用
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)

怎么解决?
我不知道如何解决它并搜索了很长时间.
如果我不清楚地描述它,只需评论,我会添加我知道的任何东西.

最佳答案 我在代码中的注释中解释了你的问题.

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!');
    });
});
点赞