无法一起执行的功能

时间:2015-12-18 10:00:59

标签: javascript node.js request cheerio

我最近正在构建一个scraper模块来获取nodejs的一些信息,直到我遇到这个" little"问题。我使用的模块是cheeriojs和request。 实际上,如果我一次只调用一个方法,那么模块就像一个魅力。它包含三个函数,只导出其中两个,这是代码:

'use strict';

var request = require('request'),
    cheerio = require('cheerio'),
    counter = 0;

function find(term, cat, callback) {
  // All the check for the parameters
  scrape("http://.../search.php?search=" + encodeURIComponent(term), cat, callback);
}

function last(cat, callback) {
  // All the check for the parameters
  scrape("http://google.com/", cat, callback);
}

function scrape(url, cat, callback) {
  request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var $ = cheerio.load(body);
      var result = [];

      var items = $('.foo, .foo2').filter(function() {
        // Condition to filter the resulted items
      });

      items.each(function(i, row) {
        // Had to do another request inside here to scrape other information
        request( $(".newpagelink").attr("href"), function(error, response, body) {
          var name = $(".selector").text(),
              surname = $(".selector2").text(),
              link = cheerio.load(body)('.magnet').attr('href'); // This is the only thing that I'm scraping from the new page, the rest comes from the other "cheerio.load"
        // Push an object in the array
        result.push( { "name": name, "surname": surname, "link": link } );

          // To check when the async requests are ended
          counter++;
          if(counter == items.length-1) {
            callback(null, result);
          }
        });
      });
    }
  });
}

exports.find = find;
exports.last = last;

正如我所说,现在的问题是,如果我创建一个新的节点脚本" test.js"我只打电话或发现,它完美无缺!但是,如果我连续调用这两种方法:

var mod = require("../index-tmp.js");
mod.find("bla", "blabla", function(err, data) {
    if (err) throw err;
    console.log(data.length + " find");
});
mod.last(function(err, data) {
  console.log(data.length + " last");
});

结果完全搞砸了,有时候剧本甚至不打印某些东西,有时打印结果只有"找到"或者"最后",其他时候会返回一个cheeriojs错误(我不会在这里添加以免弄乱你,因为可能是我的脚本错误)。我想也要为这两种方法重复相同的功能两次,但没有,同样的问题发生了......我不知道还有什么可以尝试,我希望你能告诉我这种行为的原因!

2 个答案:

答案 0 :(得分:3)

您的counter变量是全局变量,并非针对每个scrape来电。如果您同时拨打find两次,或last,则无效。

var counter = 0;的声明和初始化移至scrape函数,或者更好地在resultitems声明旁边。

答案 1 :(得分:0)

从快速扫描代码,这可能是由于变量counter是全局的。这些是异步函数,因此它们都会在同一事件上对counter起作用。将声明移到scrape函数内。

如果您需要有关异步编程的更多信息,请参阅this question中Felix的精彩答案。