2

Getting this error while checking whether the user already voted or not. I suggest setheader thing is responsible.

CODE

index : function(req, res, next){ 
    if(req.method == "POST"){
        var aa =  Users.findOneByEmail(req.param('email'), function(err, data, next){
            if(err) res.serverError(err);
            console.log(data);
            if(data){
                res.redirect('/users');
                return;
            }
        });

        var data = {
            remoteip:  req.ip,
            challenge: req.body.recaptcha_challenge_field,
            response:  req.body.recaptcha_response_field
        };

        // console.log(data);
        var recaptcha = new Recaptcha(PUBLIC_KEY, PRIVATE_KEY,  data);
        recaptcha.verify(function(success, error_code) {
            if(!success){
                return res.serverError(error_code)              
            } else {
                var username =  req.param('username');
                var vote =  req.param('vote');
                var email = req.param('email');
                var reason =  req.param('reason');
                console.log('I am here 2');
                Users.create({
                    username: req.param('username'),
                    email: email,
                    candidate_id : vote,
                    reason : reason
                }).done(function(err, data){
                    if(err){
                        return res.serverError(JSON.stringify(err));
                    } else {
                        return res.send('You sucessfully voted');
                    }
                });
            }

            res.redirect('/candidates/');
        });          
    } else {
        var recaptcha = new Recaptcha(PUBLIC_KEY, PRIVATE_KEY);
        Candidates.find().done(function(err, data){
            res.view({candidates : data, recaptcha_form: recaptcha.toHTML()});
        });
    }
},

The error:

http.js:691     
    throw new Error('Can\'t set headers after they are sent.');
Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
    at ServerResponse.res.setHeader (/usr/local/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/patch.js:59:22)
    at ServerResponse.res.set.res.header (/Users/korotane/Documents/node_projects/the_next_indian_pm/node_modules/sails/node_modules/express/lib/response.js:522:10)
    at ServerResponse.res.location (/Users/korotane/Documents/node_projects/the_next_indian_pm/node_modules/sails/node_modules/express/lib/response.js:656:8)
    at ServerResponse.res.redirect (/Users/korotane/Documents/node_projects/the_next_indian_pm/node_modules/sails/node_modules/express/lib/response.js:698:8)
    at /Users/korotane/Documents/node_projects/the_next_indian_pm/api/controllers/UsersController.js:75:14
    at IncomingMessage.<anonymous> (/Users/korotane/Documents/node_projects/the_next_indian_pm/node_modules/recaptcha/lib/recaptcha.js:160:20)
    at IncomingMessage.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickDomainCallback (node.js:459:13)
exebook
  • 32,014
  • 33
  • 141
  • 226
user3244272
  • 21
  • 1
  • 3
  • possible duplicate of [Node.js Error: Can't set headers after they are sent](http://stackoverflow.com/questions/7042340/node-js-error-cant-set-headers-after-they-are-sent) – Jérémie Parker Feb 02 '14 at 22:10

2 Answers2

3

Take a look at the line #4:

if(err) res.serverError(err);

The thing is, res.serverError sends headers already, so since you are not interrupting the execution on it, any of the following calls can cause your error: res.serverError, res.send, res.redirect.

In your particular case it's the redirection. The corresponding line in the backtrace:

...
at ServerResponse.res.redirect (/Users/korotane/Documents/node_projects/the_next_indian_pm/node_modules/sails/node_modules/express/lib/response.js:698:8)
...

Long story short, the quick fix would be adding return to the forth line:

if (err) return res.serverError(err);

That said, there's no guarantee everything else will work seamlessly.

bredikhin
  • 8,875
  • 3
  • 40
  • 44
0

In a complex code whenever you want to send a response at many occasion you can put a check res.headersSent is false.

IE:if(res.headersSent){res.send()}

if you used "return" before wile sending response then the code will not execute further.

raj23024
  • 13
  • 4