Sails.js Can't set headers after they are sent

故事扮演 提交于 2020-01-05 00:52:30

问题


I am using sailsjs v0.10.5.

I am trying to redirect to login after verifying user email and update the database before redirect.

I am using redirection in my update callback. But it sending the error after updating the database

'Cant send headers after they are sent'. 

The following is the code am using for redirection:

verifyEmail: function(req, res){
var userId = req.param('userId');
User.update({id: userId},{isVerified: true}).exec(function(err, user) {
  if (!err) { 
      req.flash('error', 'Your email is verified please login');
      res.redirect('/login'); }else { return res.send(user, 400); }
  });

回答1:


Update waterline function is asynchronous, are you sure there isnt some res method later in the scope that may be fired before?




回答2:


Its recommended to use return res.* for so-called "terminal methods" see http://sailsjs.org/#/documentation/reference/res/res.forbidden.html?q=notes



来源:https://stackoverflow.com/questions/26424515/sails-js-cant-set-headers-after-they-are-sent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!