Friday, 12 May 2017

Node disconnects the and say: Can't set headers after they are sent

events.js:160
      throw er; // Unhandled 'error' event
      ^
Error: Can't set headers after they are sent.
register.js:20:18

register.js

  user.save(function(err) {
    if(err){
      return mainFunctions.sendError(res, req, err, 500, 'error register')
    } else{
      mainFunctions.sendCheck(res, 'Register successful', 200);
      return res.redirect('/');
    }

mainFunctions.js

module.exports = {
  sendError: function(res, err, status=500, stringLog='withOutStringLog'){
    log.error(stringLog + err);
    res.setHeader('Content-Type', 'application/json');
    res.status(status).send(JSON.stringify({ err }));
  },
  sendCheck: function(res, stringLog='withOutStringLog', status=200){
    log.info(stringLog);
    res.setHeader('Content-Type', 'application/json');
    res.status(status).send(JSON.stringify({ stringLog }));
  }
}

P.S: And in server.js (main file app), I wrote app.set('json spaces', 0);



via ALPHA

No comments:

Post a Comment