简体   繁体   中英

Can't Set Headers After They Are Sent in nodejs and mysql

I am submitting login form and checking whether the username or password is correct but here I am stuck in given error please help me out to solve this error. Thanks

app.post('/login', function(request, response) {

  var username = request.body.username;
  var password = request.body.password;
  if (username && password) {
    connection.query('SELECT * FROM users WHERE username = ? AND password = ?', [username, password], function(error, results, fields) {
      console.log(results);
      if (results.length > 0) {
        request.session.loggedin = true;
        request.session.username = username;
        // request.session.w_id = us;
        response.redirect('/');
        next();
      } else {
        response.render('login', {layout: 'login',error:'Invalid Username Or Password'});
      }         
      response.end();
    });
  } else {
    response.render('login', {layout: 'login',error:'Please Enter Username And Password'});
    response.end();
  }
});

Error

throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:491:11)
    at ServerResponse.setHeader (_http_outgoing.js:498:3)
    at ServerResponse.header (D:\Node Projects\uni_project\node_modules\express\lib\response.js:775:10)
    at ServerResponse.contentType (D:\Node Projects\uni_project\node_modules\express\lib\response.js:603:15)
    at ServerResponse.send (D:\Node Projects\uni_project\node_modules\express\lib\response.js:149:14)
    at done (D:\Node Projects\uni_project\node_modules\express\lib\response.js:1012:10)
    at Immediate._onImmediate (D:\Node Projects\uni_project\node_modules\express-handlebars\lib\utils.js:26:13)
    at runCallback (timers.js:810:20)
    at tryOnImmediate (timers.js:768:5)
    at processImmediate [as _immediateCallback] (timers.js:745:5)

Problem is in this block

  if (results.length > 0) {
    request.session.loggedin = true;
    request.session.username = username;
    // request.session.w_id = us;
    response.redirect('/');
    next();
  } else {
    response.render('login', {layout: 'login',error:'Invalid Username Or Password'});
  }         
  response.end();

Once you are inside the if loop, and it's successful you've redirected and your response has ended. But Javascript will continue to the next line outside the if loop to response.end() and that's why you get the error stated. You also cannot invoke next() because this implies that the request needs further processing where infact your request is fulfilled when you invoke response.end() or response.redirect

The correct one would be -

app.post('/login', function(request, response, next) {

 var username = request.body.username;
 var password = request.body.password;
 if (username && password) {
   connection.query('SELECT * FROM users WHERE username = ? AND password = ?',[username, password], function(error, results, fields) {
    console.log(results);
   if (results.length > 0) {
    request.session.loggedin = true;
    request.session.username = username;
    // request.session.w_id = us;
    response.redirect('/');
  } else {
    response.render('login', {layout: 'login',error:'Invalid Username Or Password'});
  }         

} 
else {
response.render('login', {layout: 'login',error:'Please Enter Username And Password'});

}
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM