发送后无法设置标头

时间:2014-11-16 20:00:02

标签: node.js sails.js

我试图从this video复制Sails应用程序 这是我的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
            });
        });
    }
};

当我尝试访问show function http://localhost/user/show/4时,我收到以下错误:

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)

我无法为此提供任何解决方案。有谁可以帮我解决这个问题?

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

1 个答案:

答案 0 :(得分:0)

我没有看到重复标头的发送位置,但如果我是你,我会尝试重写你的show handler看起来像这样:

'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流,您可以保证只运行其中一个语句。我试一试,看看它是否解决了你的问题,然后从那里继续调试。