TypeError: Callback is not a function with asyncJS in nodeJS

TypeError: Callback is not a function with asyncJS in nodeJS

我想写一个通过多个同步调用调用自身的函数(这里的代码只是流程的一个剥离示例)。问题是 nodeJS 给了我一个 "TypeError: asyncCb is not a function"。

我研究了这个错误,似乎参数返回的不是函数,但我在代码中找不到错误。我是一名新的 nodeJS 开发人员,所以我可能会在这里错过一些明显的东西......

感谢您的帮助!

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);

您的 asyncTester 函数需要 4 个参数:

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

但是,您只用三个来调用它:

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

所以第四个参数 asyncCbundefined

感谢精简版。抛出错误是因为您将空值变量作为函数调用。

当您自己调用 asyncTester 时,您为 asyncCb 提供了 null,而您为 async.series 提供的函数提供了一个实际的回调函数。

我已经更新了您的代码并添加了一些 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
);

原件输出 console.log:

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

如果您根本不想提供回调,您可以像这样保护您的 return 语句:

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