2016-04-29 6 views
0

複数の同期呼び出しでコード自体を呼び出す関数を記述したいと思います(コードはここでは流れの例です)。問題は、nodeJSが私に "TypeError:asyncCb is not a function"を与えていることです。TypeError:nodeJSにasyncJSを持つ関数がコールバックではありません

このエラーを調査したところ、パラメータは関数以外の何かを返すようですが、コードでエラーが見つかりません。私は新しいnodeJSの開発者ですので、ここでは何かを見逃すかもしれません...

ありがとうございました!あなたは3つだけでそれを呼んでいる、

var asyncTester = function(number, deepdive, cb, asyncCb) { 

しかし:

var async = require('async'); 
 

 
//number is an integer, deepdive defines if the function is called the first time or not, cb contains the regular callback, asynCb contains the asyncJS callback 
 
var asyncTester = function(number, deepdive, cb, asyncCb) { 
 
    //check if function should nest itself -- only on the first call 
 
    if (deepdive == true) { 
 
     var funcArray = []; 
 
     //load async with multiple calls of this function 
 
     for (var times = 2; times < 4; times++) { 
 
      funcArray.push(function(callback) { 
 
       asyncTester(times, false, null, callback); 
 
      }); 
 
     } 
 
     //call async with array of functions and final callback handling 
 
     async.series(funcArray, 
 
      function(err, results) { 
 
       //return the original callback with the results from the async series 
 
       return cb(err, results); 
 
      }); 
 
    } 
 
    //return the async callback when in a nested call 
 
    return asyncCb(null, number); 
 
}; 
 

 
asyncTester(1, true, function(err, data) { 
 
    //expect array of results with 1, 2, 3 
 
    console.log(data); 
 
}, null);

+0

あなたがプリロードされた引数を持つ関数をバック渡していることを考えるように参照してください。 'asyncTester(...を実行するときはいつでも) 'あなたはその関数を呼び出しています*デフォルトでは関数は' undefined'を返します。したがって、これらの呼び出しのそれぞれでは、関数の代わりに 'undefined'を返しています。 –

+0

どうすればそれを避けることができますか?たとえ関数が無名関数宣言でラップされたとしても、エラーは残ります。例: for(var times = 2; times> 0; times--){ funcArray.push(function(callback){ asyncTester (2、偽、ヌル、コールバック); } ); } ' – Andre

+0

関数を呼び出すたびに新しいコールバックが渡されることを期待しているので、' funcArray.push(function(callback){asyncTester(2、false、null、callback);})配列内にあります。 'funcArray.push(function(){asyncTester(2、false、null、asyncCb);})' –

答えて

1

お買いものに役立つ情報交換がありません。ヌル値の変数を関数として呼び出すため、エラーがスローされます。

あなたがasync.seriesに与える機能は、実際のコールバック関数を提供しながら、あなたは、asyncCbためnullを提供自分asyncTesterを呼び出します。

私はあなたのコードを更新し、その点を説明するためにいくつかのconsole.logステートメントを追加しました。 console.logと、元の

var async = require('async'); 

// number is an integer, deepdive defines if the function is called the 
// first time or not, cb contains the regular callback, asynCb contains 
// the asyncJS callback 
var asyncTester = function(number, deepdive, cb, asyncCb) { 

    console.log(asyncCb); // <=== new 

    //check if function should nest itself -- only on the first call 
    if (deepdive == true) { 
     var funcArray = []; 

     //load async with multiple calls of this function 
     for (var times = 0; times < 4; times++) { 
      funcArray.push(function(callback) { 
       asyncTester(times, false, null, callback); 
      }); 
     } 

     //call async with array of functions and final callback handling 
     async.series(funcArray, 
      function(err, results) { 
       //return the original callback with the results from 
       // the async series 
       console.log('.series callback'); // <=== new 
       return cb(err, results); 
      }); 
    } 

    //return the async callback when in a nested call 
    return asyncCb(null, number); 
}; 

asyncTester(
    1, true, 
    function(err, data) { console.log(data); }, 
    function()   { console.log('all done!'); } // <=== new 
); 

出力:

null 
[Function] 
[Function] 
[Function] 
[Function] 
.series callback 
[ 4, 4, 4, 4 ] 
/Users/pmidge/workspace/personal/jstest/main.js:2079 
     return asyncCb(null, number); 
      ^

TypeError: asyncCb is not a function 
    at asyncTester (/Users/pmidge/workspace/personal/jstest/main.js:2079:16) 
    at test63 (/Users/pmidge/workspace/personal/jstest/main.js:2082:5) 
    at Object.<anonymous> (/Users/pmidge/workspace/personal/jstest/main.js:2090:1) 
    at Module._compile (module.js:413:34) 
    at Object.Module._extensions..js (module.js:422:10) 
    at Module.load (module.js:357:32) 
    at Function.Module._load (module.js:314:12) 
    at Function.Module.runMain (module.js:447:10) 
    at startup (node.js:139:18) 
    at node.js:999:3 

代わりにヌルの機能を備えた出力:

[Function] 
[Function] 
[Function] 
[Function] 
[Function] 
.series callback 
[ 4, 4, 4, 4 ] 
initiating call done! 

私はすべてことを、この問題のあなたの調査は事実によって妨げた疑いこの例では、実際には非同期ではありません。

私たちは、あなたがasync.seriesに与えられた配列にプッシュする匿名関数のボディのために、次のコードを置き換えることにより、あなたの内側のコードの非同期実行を強制することができます、

setImmediate(function() { 
    asyncTester(times, false, null, callback); 
}); 

次にコンソール出力は次のようになりますし、あなたはプロブにしたくない場合は

null 
/Users/pmidge/workspace/personal/jstest/main.js:2079 
     return asyncCb(null, number); 
      ^

TypeError: asyncCb is not a function 
    at asyncTester (/Users/pmidge/workspace/personal/jstest/main.js:2079:16) 
    at test63 (/Users/pmidge/workspace/personal/jstest/main.js:2082:5) 
    at Object.<anonymous> (/Users/pmidge/workspace/personal/jstest/main.js:2090:1) 
    at Module._compile (module.js:413:34) 
    at Object.Module._extensions..js (module.js:422:10) 
    at Module.load (module.js:357:32) 
    at Function.Module._load (module.js:314:12) 
    at Function.Module.runMain (module.js:447:10) 
    at startup (node.js:139:18) 
    at node.js:999:3 

:あなたはすぐに代わりのイベントループの将来の繰り返しでを実行するコードにバグがあることをより明確にそれをですIDEですべてのコールバック、あなたはこのようなあなたのreturn文をただ守ることができます。

//return the async callback when in a nested call 
return asyncCb 
    ? asyncCb(null, number) 
    : null; 
+0

ありがとう!私は最初の関数呼び出しがasync.series呼び出しに従ってコールバック関数を返すことを期待していたので、 "return asyncCb(null、number)"行に到達することはありません。 – Andre

+0

@Andreよろしくお願いします。 Nodeのイベントループの周りに頭を浮かべるためのテストプログラムを書く価値があります。 –

0

あなたasyncTester機能は4つの引数を期待し

asyncTester(1, true, function(err, data) { 

ので、4番目の引数asyncCbundefined

+0

これは問題ではありません。すべての引数を指定して呼び出しても、エラーは残ります。 – Andre

関連する問題