Alfiron
Alfiron

Reputation: 23

ERR_EMPTY_RESPONSE node.js after adding Integer to create entry

I use node.js express and sails.js to build my app.

When I added another input into my view it crashed on save.

ERR_EMPTY_RESPONSE

create: function(req, res){
    var title = req.body.title;
    var body = req.body.body;
    var kUser = req.body.kUser;

    Knowlege.create({title:title, body:body, kUser:kUser}).exec(function(err){ // kUser:kUser seems to be the problem
        if(err){
            res.send(500, {error: 'Database Error'});
        }
        res.redirect('/knowlege/list');
    });

Error in terminal:

Error: Can't set headers after they are sent.
at validateHeader (_http_outgoing.js:489:11)
at ServerResponse.setHeader (_http_outgoing.js:496:3) (...)

Model:

 module.exports = {
  attributes: {
    title:{
      type: 'string'
    },
    body:{
      type: 'text'
    },
    kUser:{
      type: 'integer',
      length: '11'
    }
  },
  connection:'MysqlServer'
};

Upvotes: 2

Views: 150

Answers (1)

Nir Levy
Nir Levy

Reputation: 12953

The error Can't set headers after they are sent means that you are trying to do something with your response after it was already sent.

Your problem is that you don't end the flow after sending the response in case of an error in your callback.
Since you don't, the redirection happens too, but the response was already sent.

Simply add return to exit the function:

if(err){
    return res.send(500, {error: 'Database Error'});
}
res.redirect('/knowlege/list');

Upvotes: 2

Related Questions