2015-11-21 121 views
0

我一直在浏览此代码一段时间,并搜索了许多其他来源,并且我知道此问题已被多次回答。我尝试过多种方法(向res.render添加返回语句,注释res.render等),我无法弄清楚为什么我会抛出“发送后无法设置标题”错误。“发送后无法设置标题”

下面是代码:

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); 
}); 

有没有人有一个想法,为什么我得到这个错误?

+0

响应开始时必须写出标题。 – Pointy

+0

您是否在使用任何可能向客户端发送响应的中间件? –

回答

0

你仍然可能渲染两次。

一旦与res.render('toDo', context);你的路由处理的最末端(始终执行),然后再在代码路径next(err);您的应用程序将在一个这样或那样的(例如,如果没有其他的路由处理相匹配,快速将响应返回404)。

+0

感谢您的输入!那么,我应该在最后消除'res.render('toDo',context)'吗? –

+0

这取决于你的应用程序。快速解决方法是在if(req.body ['Add Item']){...}'块的末尾(内部)添加一个'return;',除非'req.body [''添加项目']和'req.body ['Done']'都可以同时设置,在这种情况下,您的'req.session.toDo'不会被更新... – mscdex

+0

有一个渲染功能“res.render('toDo',context)”位于回调函数中:getWeather。根据当前的结构,我们不能有任何渲染功能。请参阅https://github.com/kenpeter/can-not-send-header – kenpeter

相关问题