简体   繁体   English

TypeError:object不是async.waterfall的函数

[英]TypeError: object is not a function with async.waterfall

I am a node.js noob trying to use async.waterfall . 我是一个node.js noob试图使用async.waterfall I have problems to get from the last task of the waterfall array to the final callback method. 从瀑布数组的最后一项任务到最终的回调方法,我遇到了问题。

In the example below I am passing the callback to doSomethingAsync , but when I want to execute the callback inside doSomethingAsync I get TypeError: object is not a function . 在下面的示例中,我将回调传递给doSomethingAsync ,但是当我想在doSomethingAsync执行回调时,我得到TypeError: object is not a function I don't understand. 我不明白。 Thank you for your thoughts 谢谢你的想法

EDIT : 编辑

The first task of the waterfall is the creation of a Mongo document. 瀑布的第一个任务是创建Mongo文档。 The callback of the save() function is function(err){...} . save()函数的回调是function(err){...}

var session = createSession(); // session is a Mongoose model
async.waterfall([

    function (callback) {
        ...
        session.save(callback); // Model.save(function(err){...}
    },

    function (callback) {
        doSomethingAsync(session, callback)
    }

], function (err, session) {


});

function doSomethingAsync(session, callback){
    doSomething(function(err){
        callback(err,session);
    }
}


 callback(err,session);
 ^
 TypeError: object is not a function

My guess is that the problem lies in code you have removed. 我的猜测是问题出在您已删除的代码中。 More specifically, you probably had a function in the waterfall right before the one you've shown that calls doSomethingAsync() . 更具体地说,你可能在你已经显示调用doSomethingAsync()之前的瀑布中有一个函数。

The way async.waterfall() works is that it passes on any arguments passed to the callback to the next function in the function list. async.waterfall()工作方式是将传递给回调的所有参数传递给函数列表中的下一个函数。 So the previous function is probably doing something like callback(null, { foo: 'bar' }) and your callback argument in the next function is actually { foo: 'bar' } and the second argument is the real callback. 所以前面的函数可能正在执行callback(null, { foo: 'bar' }) ,下一个函数中的callback参数实际上是{ foo: 'bar' } ,第二个参数是真正的回调。 It really depends on how many arguments you passed previously to the callback. 这实际上取决于您之前传递给回调的参数数量。

So assuming you just pass 1 item, you would change the function definition from: 因此,假设您只传递了1个项目,您将更改函数定义:

function (callback) {
    doSomethingAsync(session, callback)
}

to: 至:

function (someResult, callback) {
    doSomethingAsync(session, callback)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM