简体   繁体   English

错误:发送Node.js和ExpressJS后无法设置标头

[英]Error: Can't set headers after they are sent Node.js and ExpressJS

I am using Node.js and ExpressJS to save a mongoose model. 我正在使用Node.js和ExpressJS保存猫鼬模型。 I am getting the error of Error: Can't set headers after they are sent. 我收到Error: Can't set headers after they are sent. I think it has to do with the line res.redirect("/dashboard/it/model"); 我认为这与行res.redirect("/dashboard/it/model"); conflicting with setRedirect({auth: '/login'}), from my route, the code in the setRedirect is below labeled. setRedirect({auth: '/login'}),冲突setRedirect({auth: '/login'}),从我的路线来看,setRedirect中的代码在下面标记。 The setRedirect is from a middleware called middleware-responder that came as part of the template I am using to create Stripe user accounts. setRedirect来自称为middleware-responder程序的中间件,该中间件是我用来创建Stripe用户帐户的模板的一部分。 I can not remove it as far as I know. 据我所知,我无法将其删除。 my GitHub repo I have committed all files that are not test files and are relevant (no unneeded views ect than what is already there) 我的GitHub存储库中,我已提交了所有不是测试文件且相关的文件(没有多余的视图等已存在的视图)

Save Model 保存模型

if(type=="aps"){
      var newAccessPoint = {
          name: name,
          manufacturer: manufacturer,
          model: model,
          range: range, 
          bands: bands,
          channel: channel,
          poe: poe,
          notes: notes,
          signout: signout,
          author:author
        };
      // Create a new access point and save to DB
      AP.create(newAccessPoint, function(err, newlyCreated){
          if(err){
              console.log(err);
          } else {
              //redirect back to models page
              res.redirect("/dashboard/it/model");
          }
      });
    }

Route 路线

app.post('/dashboard/it/model/new',
    setRender('dashboard/it/modelCreate'),
    setRedirect({auth: '/login'}),
    isAuthenticated,
    dashboard.getDefault,
   (req, res) => {

setRedirect code setRedirect代码

exports.setRedirect = function(options){
  return function(req, res, next){
   if(req.redirect){
     req.redirect = _.merge(req.redirect, options);
   } else {
     req.redirect = options;
   }
   next();
 };
};

setRender code setRender代码

exports.setRender = function(view){
  return function(req, res, next){
    req.render = view;

    next();
  };
};

That's happening because you are trying to send a response to the client when you already closed the connection. 发生这种情况是因为您已经尝试在关闭连接后尝试向客户端发送响应。

Hard to tell by the way you are showing us your code but it seems like you are redirecting to options and then in the same request you are redirecting to dashboard/it/model 很难通过向我们展示代码的方式来区分,但是似乎您正在重定向到选项,然后在同一请求中您将重定向到仪表板/它/模型

I pull your code from github. 我从github提取您的代码。 I think the error message was clear. 我认为错误消息很清楚。 in your getDefault() middleware you are rendering a response so the server start sending data to your client and just after you try to redirect him to another url. getDefault()中间件中,您正在呈现响应,以便服务器开始将数据发送到您的客户端,并且在您尝试将其重定向到另一个URL之后。 Thats why when your comment out that middleware all work nicely. 这就是为什么当您注释掉该中间件时,它们都能很好地工作的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM