node.js for 循环在函数调用之前迭代 returns 因此创建对象问题

node.js for loop iterating before the function call returns hence creating object issue

我有节点代码可以获取要抓取的网站列表,并在遍历结果时调用使用 phantom 编写的抓取函数。但是在爬行函数 returns 结果之前,循环迭代了多次,因此调用了无法处理它的爬行函数的次数。我需要立即回答我的问题。 请有人带我离开这口井。

我的主页代码

    db.fetch_serviceEntity(function(serviceEntityData){
      if(serviceEntityData!=""){
        serviceEntityData.forEach(function(item){
            console.log(item.website_url);
            db.fetch_entityId(item.id,function(entityId){
                crawler.getCount(item.website_url, item.name, function(rCount){
                    console.log("number of fresh reviews to crawl : ", parseInt(rCount) - parseInt(item.review_count));
                    if(rCount > item.review_count){
                        fetchReviews(item.website_url, entityId.id, parseInt(rCount) - parseInt(item.review_count), function(){
                            db.updateReviewCount(item.id, rCount, function(){
                                process.exit(0);    
                            });
                        });
                    }
                });
             });
          };    
        }); 
      }
      else {
        console.log("No websites to crawl/database error");
      }
      process.exit(0);
    });

我的抓取功能来了

    crawler.prototype.crawl = function(webUrl, callback){
    console.log(webUrl);
    this.driver.create({ path: require('phantomjs').path }, function (err, browser) {
      return browser.createPage(function (err,page) {
        return page.open(webUrl, function (err,status) {
          console.log("opened site? ", status);
          page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js', function (err) {
            // Wait for a bit for AJAX content to load on the page. Here, we are waiting 5 seconds.
            setTimeout(function () {
              return page.evaluate(function () {
                //Get what you want from the page using jQuery.
                var reviews = [];
                $('li div.review').each(function () { 
                  if($(this).find('div.ypassport').text()){
                    var d = new Date($(this).find('span.rating-qualifier').text().trim());
                    var temp = {
                        id : $(this).attr('data-review-id'), 
                        entity_id : "",
                        user_id : $(this).attr('data-signup-object').split(":")[1],
                    }
                    reviews.push(temp);
                  }
                });

                return {
                  reviews: reviews
                };
              }, function (err,result) {
                browser.exit();
                callback(result);
              });
            }, 5000);
          });
        });
      });
    });

};

我正在使用node-phantom-simple写爬虫功能

我的问题是 -> 因为 for 循环对它进行了多次调用,爬网函数给我错误消息,指出某些或其他对象未创建。 例如在代码中它说 "createpage is not a function of undefined" 因此意思是没有创建浏览器对象。 有时它说 "open is not a function of undefined " 因此 "page" 对象没有创建。

你有异步函数,但是如果你 process.exit(0) 当你从你的第一个函数 return 时,所有数据库连接都被删除并且没有 db.updateReviewCount 被调用。因此,您或多或少会得到任意结果,具体取决于第一个关闭的人如何。

(除此之外,代码是一个回调地狱。也许您想创建更小的函数,然后将它们与 asyncco 之类的库链接起来,甚至手动链接。)