发送后无法设置标头

问题描述:

我正在尝试从这个视频.
这是我的 UserController:

module.exports = {
    'new': function(req, res) {
        res.view()
    },
    'create': function(req, res, next) {
        User.create(req.params.all(), function userCreated(err, user) {
            if(err) {
                console.log(err);
                req.session.flash = {
                    err: err
                }
                return res.redirect('/user/new');
            }
            //res.redirect('/user/show/'+user.id);
            res.json(user);
        });
    },
    'show': function(req, res, next) {
        User.findOne(req.param('id'), function foundUser(err, user) {
            if(err) return next(err);
            if(!user) return next();
            res.view({
                user: user
            });
        });
    }
};

当我尝试访问显示功能 http://localhost/user/show/4 时,出现以下错误:

When I am trying to access the show function http://localhost/user/show/4 I am getting below 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 (/home/codio/.nvm/v0.10.25/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/patch.js:63:22)   
    at next (/home/codio/.nvm/v0.10.25/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:158:13)                          
    at resume (/home/codio/.nvm/v0.10.25/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/static.js:65:7)              
    at SendStream.error (/home/codio/.nvm/v0.10.25/lib/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/static.js:80:37)   
    at SendStream.EventEmitter.emit (events.js:95:17)                                                                                                 
    at SendStream.error (/home/codio/.nvm/v0.10.25/lib/node_modules/sails/node_modules/express/node_modules/send/lib/send.js:147:51)                  
    at SendStream.onStatError (/home/codio/.nvm/v0.10.25/lib/node_modules/sails/node_modules/express/node_modules/send/lib/send.js:248:48)            
    at /home/codio/.nvm/v0.10.25/lib/node_modules/sails/node_modules/express/node_modules/send/lib/send.js:320:26                                     
    at Object.oncomplete (fs.js:107:15)

我无法为此提供任何解决方案.任何人都可以帮我解决这个问题吗?

I am not able to fund out any solution for that. Can anyone please help me to resolve this issue?

注意:我使用的是 Sails.Js 版本:v0.10.5

Note: I am using Sails.Js version : v0.10.5

我没有看到重复的标头被发送到哪里,但是如果我是你我会做的是尝试重写你的显示处理程序看起来像这个:

I'm not seeing where the duplicate header is being sent, but what I'd do if I were you is to try rewriting your show handler to look like this:

'show': function(req, res, next) {
  User.findOne(req.param('id'), function foundUser(err, user) {
    if(err) {
      return next(err);
    } else if (!user) {
      return next();
    } else {
      res.view({
        user: user
      });
    }
  });
}

通过强制 if/else if/else 流程,您保证只会运行这些语句之一.我会尝试一下,看看它是否能解决您的问题,然后从那里继续调试.

By forcing the if / else if / else flow, you GUARANTEE that only one of those statements will be ran. I'd give that a try and see if it fixes your issue, then continue debugging from there.