错误:发送标头后无法设置. Express.js [英] Error: Can't set headers after they are sent. Express.js

查看:65
本文介绍了错误:发送标头后无法设置. Express.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很长时间以来一直在节点和堆栈上遇到此错误.请帮助我解决我的问题. 这是我的代码:

Im kinda new at node and stack for a long time with this error. Please help me sovle my problem. Here is my code:

app.get('/', function(req , res){
request(url, function(err, response, html){
if (!err && response.statusCode == 200){
  var page = cheerio.load(html);
  page('.search-result-description').each(function(){
    var data = [];
    var name = page(this).find('.search-result-item__head');
    var company = page(this).find('.search-result-item__company');
    var description = page(this).find('.search-result-item__snippet');
    var requirements = page(this).find('.search-result-item__snippet');
    var salary = page(this).find('.b-vacancy-list-salary');

    data.push({
      vac_name:  name.eq(0).text(),
      vac_description: description.eq(0).text(),
      vac_requirements: requirements.eq(0).text(),
      var_company: company.eq(0).text(),
      vac_salary : salary.eq(0).text()
    });
    data = JSON.stringify(data, null, 4);
    res.render('main.ejs', {data: data});
  });
  }
});
});

推荐答案

发送http响应(如res.render())的方法每个请求只能调用一次(每个请求只有一个响应).多次调用它们时,您会收到有关已发送标题的错误消息.

Methods that send the http response like res.render() can only be called once per request (there's only one response per request). When you call them more than once, you get the error message about headers already being sent.

假设您的page()函数是同步的,则只需在循环结束后而不是在循环内移动几个语句,并将data变量的声明也移动到循环外,以便您可以累积数据循环:

Assuming your page() function is synchronous, you just need to move a couple statements after the end of the loop instead of inside the loop and move your declaration of the data variable outside the loop too so you can accumulate data with the loop:

app.get('/', function(req, res) {
    request(url, function(err, response, html) {
        if (!err && response.statusCode == 200) {
            var page = cheerio.load(html);
            var data = [];
            page('.search-result-description').each(function() {
                var name = page(this).find('.search-result-item__head');
                var company = page(this).find('.search-result-item__company');
                var description = page(this).find('.search-result-item__snippet');
                var requirements = page(this).find('.search-result-item__snippet');
                var salary = page(this).find('.b-vacancy-list-salary');

                data.push({
                    vac_name: name.eq(0).text(),
                    vac_description: description.eq(0).text(),
                    vac_requirements: requirements.eq(0).text(),
                    var_company: company.eq(0).text(),
                    vac_salary: salary.eq(0).text()
                });
            });
            res.render('main.ejs', {
               data: JSON.stringify(data, null, 4);
            });
        }
    });
});

如果request()操作返回错误,您还应该处理一些错误.从目前的情况来看,您永远不会响应请求.

You also should have some error handling if the request() operation returns an error. As it stands how, you just never respond to the request.

这篇关于错误:发送标头后无法设置. Express.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆