简体   繁体   English

无法弄清楚为什么我不断收到 [ERR_HTTP_HEADERS_SENT] 错误

[英]Can't figure out why I keep getting [ERR_HTTP_HEADERS_SENT] error

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I have seen other posts about this same issue, but I wasn't able to figure out how to fix my problem.我看过其他关于同一问题的帖子,但我无法弄清楚如何解决我的问题。 I have code handling a post request like so:我有代码处理这样的发布请求:

router.post('/', function(req, res) {
  var movies = JSON.parse(fs.readFileSync('movies.json'));
  var title = req.body.title;
  movies.forEach(function(movie) {
    if (title == movie.title) {
      return res.render('movie', { watched: false });
    } else {
      return res.render('movie', { watched: true });
    }
  });
});

I understand the issue is due to res.render() being there twice, but even with a return statement it still gives me the same error so I am having a hard time understanding how it is getting sent twice.我知道这个问题是由于res.render()出现了两次,但即使有一个return语句,它仍然给我同样的错误,所以我很难理解它是如何被发送两次的。 Shouldn't it only be sending once due to the return keyword?由于return关键字,它不应该只发送一次吗?

Also, if I modify my code like so:另外,如果我像这样修改我的代码:

router.post('/', function(req, res) {
  var movies = JSON.parse(fs.readFileSync('movies.json'));
  var title = req.body.title;
  movies.forEach(function(movie) {
    if (title == movie.title) {
      return res.render('movie', { watched: false });
    }
  });
res.render('movie', { watched: true});
});

Then I only recieve the error when the if statement is false.然后我只在 if 语句为假时收到错误。 Why is that the case?为什么会这样?

You are returning inside for loop so each time it will send您正在返回内部 for 循环,因此每次它都会发送

modify your code like below修改您的代码,如下所示

router.post('/', function(req, res) {
  var movies = JSON.parse(fs.readFileSync('movies.json'));
  var title = req.body.title;
  var counter =0;
  var arr = [];
  movies.forEach(function(movie) {
    if (title == movie.title) {
      arr.push({ watched: false , movieId:id})      
    } else {
      arr.push({ watched: true , movieId:id})
    }
    counter++;
   if(counter == movies.length){
      return res.send({'movie':arr});
   }
  });
});

Turns out the solution I needed was a lot simpler than I was thinking.事实证明,我需要的解决方案比我想象的要简单得多。 I needed to pass a boolean to the render function based on the result of the if statement, so rather than calling res.render() twice, I just created a new variable to store the result of the if statement and then feed that into res.render() so it only gets called once.我需要根据 if 语句的结果将 boolean 传递给渲染 function,因此我没有调用res.render()两次,而是创建了一个新变量来存储 if 语句的结果,然后将其输入res.render()所以它只被调用一次。

router.post('/', function(req, res) {
  var movies = JSON.parse(fs.readFileSync('movies.json'));
  var title = req.body.title;
  var isWatched = false;
  for (let value in movies) {
      if (movies[value].title === title) {
        isWatched = true;
      }
    }
  res.render('movie', { watched: isWatched} );
});

暂无
暂无

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

相关问题 ERR_HTTP_HEADERS_SENT 错误但找不到它的来源 - ERR_HTTP_HEADERS_SENT Error But Can't Find The Origin of it 这是我收到的错误错误 [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - this is the error i m getting Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client 错误 [ERR_HTTP_HEADERS_SENT]:发送到客户端后无法设置标题,我无法发送表单 - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, i can't send a form 错误 [ERR_HTTP_HEADERS_SENT]:无法在将标头发送到客户端后设置标头,我无法将表单发送到实时数据库 - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client, i can't send a form to realtime database node.js收到“ ERR_HTTP_HEADERS_SENT”错误 - node.js getting 'ERR_HTTP_HEADERS_SENT' error 获取 Mailgun 'ERR_HTTP_HEADERS_SENT' - Getting Mailgun 'ERR_HTTP_HEADERS_SENT' 我该如何修复(节点:5796)UnhandledPromiseRejectionWarning:错误[ERR_HTTP_HEADERS_SENT]:错误? - How can i fix (node:5796) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: error? 尝试重定向时出现“错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头” - Getting “Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client” when trying to redirect 错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头。 为什么? - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. Why? 登录时出现错误 [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client - When loging in I get Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM