2012-04-22 10 views
1

生成された関数を連続して呼び出す際に問題があります。私は非同期ライブラリを使用しており、深いコールバック呼び出しが必要ないときにコードが動作するようです。実際のシナリオを追加すると、エラーが発生します。ここで非同期で直列に生成された関数を呼び出す

は作品例であり、0〜4の配列を返します。

Scrape.prototype.generatePageFunctions = function() { 
    var functionList = new Array(), self = this; 

    for (var i = 0; i < this.pageSet; i++) { 
    (function(i) { 
     functionList.push(function(cb) { 
     // Inner functions which will be called in seriers 
     var timeoutTime = parseInt(Math.random() * 5000 + 3000, 10); 

     setTimeout(function() { 
      self.setIndex(i); 
      //self.getSite(function) 

      cb(null, i); 
     }, timeoutTime); 
     }); 
    })(i); 
    } 
    return functionList; 
} 

Scrape.prototype.run = function() { 
    var functionList = this.generatePageFunctions(); 

    async.series(functionList, function(err, results) { 
    console.log('Job is completed '); 
    console.log(results); 
    }); 
} 

今すぐサイトをダウンロードして、コールバックに入れのような本当のシナリオを追加:

Scrape.prototype.generatePageFunctions = function() { 
    var functionList = new Array(), self = this; 

    for (var i = 0; i < this.pageSet; i++) { 
    (function(i) { 
     functionList.push(function(cb) { 
     // Inner functions which will be called in seriers 
     var timeoutTime = parseInt(Math.random() * 5000 + 3000, 10); 

     setTimeout(function() { 
      self.setIndex(i); 
      self.getSite(function(result) { 
      // Async callback to pass the data 
      cb(null, result); 
      }); 
     }, timeoutTime); 
     }); 
    })(i); 
    } 
    return functionList; 
} 

エラーがありますこのように、結果イテレータ変数iの代わりに渡しても、イテレータ変数i:

/home/risto/scrape/node_modules/async/lib/async.js:185 
      iterator(x.value, function (err, v) { 
        ^
TypeError: Cannot read property 'value' of undefined 
    at /home/risto/scrape/node_modules/async/lib/async.js:185:23 
    at /home/risto/scrape/node_modules/async/lib/async.js:108:13 
    at /home/risto/scrape/node_modules/async/lib/async.js:119:25 
    at /home/risto/scrape/node_modules/async/lib/async.js:187:17 
    at /home/risto/scrape/node_modules/async/lib/async.js:491:34 
    at /home/risto/scrape/scraper/scrape.js:114:13 
    at /home/risto/scrape/scraper/scrape.js:64:16 
    at Object.<anonymous> (/home/risto/scrape/scraper/engines/google.js:58:12) 
    at Function.each (/home/risto/scrape/node_modules/cheerio/lib/api/utils.js:133:19) 
    at [object Object].each (/home/risto/scrape/node_modules/cheerio/lib/api/traversing.js:69:12) 

//編集

完全なコールバックに追加された結果のみが最初のものであり、他の関数は呼び出されません。 また、関数がオブジェクトリテラルを返す場合は、それが重要です。

+1

あなたはもっと役に立つ回答を受け入れるべきです。 – rekire

+0

True、その特徴を忘れました:)、固定 –

答えて

2

コードに問題はありません。シンプルなテストケースを作成するとそのことがわかります。

私はモックを作成しました:

Scrape = function() { 
    this.pageSet = 5; 
} 

Scrape.prototype.setIndex = function() { 
} 

Scrape.prototype.getSite = function(cb) { 
    cb('works'); 
} 

、それは予想を出力runメソッド呼び出し:

[ 'works', 'works', 'works', 'works', 'works' ] 

だから、問題はどこか別の場所です。 runメソッドのfunctionList変数を確認しようとしましたか?

+0

配列に[[関数]、[関数]、[関数]、[関数]、[関数]の関数が含まれていると、同様のトピックが見つかりました。エラーが発生します。 http://stackoverflow.com/questions/8023591/node-js-async-foreach-cannot-read-property-value-of-undefined –

0

ありがとうございました@KARASZIIstván、上記のコードはすべて正しいですが、問題はどこか他の場所にあるようです。最も深いコールバックが複数回呼び出されましたが、外側のコールバックは1回しか呼び出されませんでした。

関連する問題