简体   繁体   English

“发送后无法设置标题”

[英]“Can't Set Headers After They're Sent”

I have been going through this code for a while and have searched numerous other sources and I know this question has been answered many times. 我已经阅读了一段时间的代码,并且搜索了许多其他资源,并且我知道这个问题已经被回答了很多次。 I have tried multiple things(adding a return statement to my res.render, commenting out a res.render, etc.) and I cannot figure out why I am being thrown the "Can't Set Headers After They're Sent" error. 我已经尝试了多种方法(在res.render中添加了return语句,在res.render中添加了注释,等等),但我无法弄清楚为什么我被抛出“发送后无法设置标题”错误。

Here is the code: 这是代码:

app.post('/',function(req,res){
  var context = {};

  if(req.body['New List']){
    req.session.name = req.body.name;
    req.session.toDo = [];
    req.session.curId = 0;
  }

  //If there is no session, go to the main page.
  if(!req.session.name){
    res.render('newSession', context);
    return;
  }

  if(req.body['Add Item']){


    var city = req.body.city;
    var minimum = req.body.temp;

    console.log("before request");
    request('http://api.openweathermap.org/data/2.5/weather?q='+ city +'oh&appid='+myKey, getWeather);
    function getWeather(err, response, body){



        if(!err && response.statusCode < 400)
        {

            var weather = JSON.parse(body);
            var tempFahrenheit = ((weather.main.temp - 273.15)*1.8) + 32

            console.log(tempFahrenheit);
            if(tempFahrenheit >= minimum)
            {
                var doNow = "yes, do this now";
                req.session.toDo.push({"name":req.body.name, "id":req.session.curId, "city":req.body.city, "temp":req.body.temp,
                 "doNow":doNow});
                 req.session.curId++;
                 context.name = req.session.name;
                 context.toDoCount = req.session.toDo.length;
                 context.toDo = req.session.toDo;
                 console.log(context.toDo);
                 return res.render('toDo',context);


            }
            else
            {
                var doNow = "no, do this later";
                req.session.toDo.push({"name":req.body.name, "id":req.session.curId, "city":req.body.city, "temp":req.body.temp,
                 "doNow":doNow});
                 req.session.curId++;
                 context.name = req.session.name;
                 context.toDoCount = req.session.toDo.length;
                 context.toDo = req.session.toDo;
                 console.log(context.toDo);
                 return res.render('toDo',context);



            }

            res.render('toDo', context);

        }
        else
        {
            if(response)
            {
                console.log(response.statusCode);

            }
            next(err);

        }

    }

}

  if(req.body['Done']){
    req.session.toDo = req.session.toDo.filter(function(e){
      return e.id != req.body.id;
    })
  }

/*
context.name = req.session.name;
context.toDoCount = req.session.toDo.length;
context.toDo = req.session.toDo;
console.log(context.toDo);*/
console.log(context.toDo);
res.render('toDo',context);
});

Does anyone have an idea why I'm getting this error? 有谁知道为什么我会收到此错误?

You are still possibly rendering twice. 您仍然可能渲染两次。

Once with res.render('toDo', context); 一次使用res.render('toDo', context); at the very end of your route handler (which always executes) and then again in the code path to next(err); 在路由处理程序的末尾(始终执行),然后再次在代码路径中到达next(err); where your app will respond in one way or another (eg if no other route handler matches, Express will return 404). 您的应用将以一种或另一种方式响应的位置(例如,如果没有其他路由处理程序匹配,Express将返回404)。

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

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