【问题标题】:Array push takes forever in Node JS after response响应后,数组推送在 Node JS 中需要永远
【发布时间】:2016-09-21 18:58:48
【问题描述】:

我正在尝试从 yelp 中抓取并附上下面的代码。我在将数据存储到数组时遇到问题。

这是我的代码:

...
var id, title, link, neighborhood, address, phone = [];

router.get('/', function(req, res, next) {
var cheerio = require('cheerio');

while (scrapepage) {
    var options = {
        uri: 'https://www.yelp.co.uk/search?find_desc='+find+'&find_loc='+city+''+'&start='+page,
        transform: function (body) {
            return cheerio.load(body);
        }
    };

    page += 10;
    rp(options)
        .then(function ($) {


            var json = { id: "", title : "", link : "", neighborhood : "", address : "", phone : ""};                

            $('.biz-name span').filter(function(){
                var data = $(this).text();
                console.log(data);
                //title.push(data);
                title_count++;
            });

           ...

            res.send('Check your console!')
        })
        .catch(function (err) {
            // Crawling failed or Cheerio choked...
        });           
    }
});

所以每当我尝试将数据推送到数组时,它都不起作用,一直在等待。如果我删除推送,它会控制所有数据。

我也尝试过使用 each 而不是过滤器,但没有运气。也试过手动放入数组索引,还是不行。我可以知道我在代码中做错了什么吗?

更新

我已经在页面顶部添加了这个。

var id, title, link, neighborhood, address, phone = []; 

【问题讨论】:

  • title.push - 但您没有为 title 分配任何东西 - 该代码不应该与 cannot get property push from undefined 一起失败吗?
  • 我已经定义为 var title = [];
  • I have added this at the top of the page.title 仍未定义。只有phone 被分配给一个新数组,所有其他变量都只是声明但没有初始化..
  • 知道了。感谢您指出错误。

标签: javascript node.js express request cheerio


【解决方案1】:

我不得不问标题在哪里初始化?我看到了声明,但没有告诉系统将标题初始化为数组。

试试

...
router.get('/', function(req, res, next) {
var cheerio = require('cheerio');

while (scrapepage) {
    var options = {
        uri: 'https://www.yelp.co.uk/search?find_desc='+find+'&find_loc='+city+''+'&start='+page,
        transform: function (body) {
            return cheerio.load(body);
        }
    };

    page += 10;
    rp(options)
        .then(function ($) {

            var title = [], 
                release, rating;
            var json = { id: "", title : "", link : "", neighborhood : "", address : "", phone : ""};                

            $('.biz-name span').filter(function(){
                var data = $(this).text();
                console.log(data);
                title.push(data);
                title_count++;
            });

          ...

            res.send('Check your console!')
        })
        .catch(function (err) {
            // Crawling failed or Cheerio choked...
        });           
    }
});

在没有初始化的情况下,系统必须通过一个过程来确定参数的类型和兼容性,以确保它可以为您提供尽可能接近您所要求的内容。有时显式定义一个变量可以加快这个过程。

同样你不应该使用 title_count 因为 title.length 会有元素的数量。

【讨论】:

    【解决方案2】:

    push 将不起作用 untiltitle 分配为数组类型

    then(function ($) {
    
                var title=[];
                var release, rating;
                var json = { id: "", title : "", link : "", neighborhood : "", address : "", phone : ""};                
    
                $('.biz-name span').filter(function(){
                    var data = $(this).text();
                    console.log(data);
                    title.push(data);
                    title_count++;
                });
    
               ...
    
                res.send('Check your console!')
            })
    

    【讨论】:

    • 嘿,是的,它适用于title=[],我的错,但我已经在页面顶部初始化它,刚刚更新了代码。
    • 您仍然没有将标题指定为数组
    • 嘿,是的,明白了。我的错。感谢您指出错误。
    猜你喜欢
    • 2013-02-17
    • 2018-01-02
    • 2018-02-27
    • 2018-03-13
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2018-07-14
    • 2013-12-08
    相关资源
    最近更新 更多