setheader给出错误:“引发新错误('在发送标头后无法设置标头.');" [英] setheader gives error: "throw new Error('Can\'t set headers after they are sent.');"
问题描述
在检查用户是否已投票时得到此错误.我建议 setheader
这件事负责.
Getting this error while checking whether the user already voted or not. I suggest setheader
thing is responsible.
代码
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()});
});
}
},
错误:
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)
推荐答案
看看第4行:
if(err) res.serverError(err);
问题是, res.serverError
已经发送了标头,因此,由于您没有中断在其上的执行,因此以下任何调用都可能导致您的错误: res.serverError,res.send,res.redirect
.
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)
...
长话短说,快速的解决方法是将 return
添加到第四行:
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.
这篇关于setheader给出错误:“引发新错误('在发送标头后无法设置标头.');"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!