4

I use mysql and express to write a easy login system.

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

However, everytime when i get this error:

/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)

How to solve it? I do not know how to solve it and search for a long time. If i describe it not clearly, just comment and i will add anything i know.

1
  • I think your problem is not with mysql but it's that you are trying to change response object after sending it to user. (see stack trace) Commented May 14, 2016 at 8:28

3 Answers 3

1

I explained your problem in comments in code.

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!');
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

You have issues in the following snippet.

if(req.body.password===manager.password){
   req.session.manager = manager;
   res.redirect('/home');
}
res.send('ID or password wrong!');

It is clear from error stack trace that you are trying to set headers after it is being sent. It means that if statement executes first and if it true, it redirect to /home, after that it exits the if clause and tries to send message to user.

Possible Solution

if(req.body.password===manager.password){
   req.session.manager = manager;
   res.redirect('/home');
} else {
   res.send('ID or password wrong!');
}

Check it, if it helps you.

Comments

0

I should return in the if:

if(req.body.password===manager.password)       
 {
    req.session.manager = manager;
    res.redirect('/home');
    return;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.