res.send发送后无法设置标头

时间:2015-09-28 14:20:17

标签: node.js express

您好我有以下方法,我无法确定我收到此错误的原因

  

错误:发送后无法设置标头。       在ServerResponse.OutgoingMessage.setHeader(_http_outgoing.js:335:11)       在ServerResponse.header(/home/kwanso/Projects/myApp/Server/node_modules/express/lib/response.js:718:10)       在ServerResponse.json(/home/kwanso/Projects/myApp/Server/node_modules/express/lib/response.js:246:10)       在/home/kwanso/Projects/myApp/Server/app.js:92:11       在Layer.handle [as handle_request](/home/kwanso/Projects/myApp/Server/node_modules/express/lib/router/layer.js:95:5)       在下一个(/home/kwanso/Projects/myApp/Server/node_modules/express/lib/router/route.js:131:13)       完成后(/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/middleware/authenticate.js:243:13)       at /home/kwanso/Projects/myApp/Server/node_modules/passport/lib/middleware/authenticate.js:250:15       通过(/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:421:14)       在Authenticator.transformAuthInfo(/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:443:5)       at /home/kwanso/Projects/myApp/Server/node_modules/passport/lib/middleware/authenticate.js:247:22       at /home/kwanso/Projects/myApp/Server/node_modules/passport/lib/http/request.js:58:7       通过(/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:267:43)       在序列化(/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:276:7)       在/home/kwanso/Projects/myApp/Server/app.js:97:3       通过(/home/kwanso/Projects/myApp/Server/node_modules/passport/lib/authenticator.js:284:9)    - 来自/home/kwanso/Projects/myApp/Server/node_modules/express/lib/application.js:629:43(功能   EventEmitter.logerror)

我的方法如下:

passport.use(new LocalStrategy(
      function(username, password, done) {
        console.log("New Local Strategy");
        User.find({ email: username }, function (err, user) {
          console.log(user[0]._doc);
          if (err) { return done(err); }
          if (!user) { return done(null, false); }
          if (user[0]._doc.password != password) { return done(null, false); }
          console.log("sending");
          return done(null, user);
          console.log("sendt");
        });
        return done(null , false);
      }
    ));

    var isValidPassword = function(user, password){
      return true;
    }

    app.post('/login',
    passport.authenticate('local') ,
    function(req, res) {
      console.log("Back");
          res.send(req.user[0]);
    });

    passport.serializeUser(function(user, done) {
      console.log("Serialize User");
      done(null, user);
    });

    passport.deserializeUser(function(user, done) {
      console.log("De Serialize User");
      done(null, user);
    });

这似乎与在回调中的回调方法中调用res.send有关。这是一个问题吗?

1 个答案:

答案 0 :(得分:4)

你在LocalStrategy Twice中调用完成回调

一旦在函数结束时(用户名,密码,已完成){

  if (err) { return done(err); }
  if (!user) { return done(null, false); }
  if (user[0]._doc.password != password) { return done(null, false); }
  console.log("sending");
  return done(null, user);

和从数据库中提取用户后的一次

  return done(null , false);

只需删除最后一行即可获得

var out_width =  $(window).width();