javascript - TypeError: object is not a function with async.waterfall - Stack Overflow

I am a node.js noob trying to use async.waterfall.I have problems to get from the last task of the wat

I am a node.js noob trying to use 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. I don't understand. Thank you for your thoughts

EDIT:

The first task of the waterfall is the creation of a Mongo document. The callback of the save() function is 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

I am a node.js noob trying to use 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. I don't understand. Thank you for your thoughts

EDIT:

The first task of the waterfall is the creation of a Mongo document. The callback of the save() function is 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
Share Improve this question edited Feb 9, 2015 at 13:32 znat asked Feb 9, 2015 at 1:34 znatznat 13.5k18 gold badges76 silver badges111 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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().

The way async.waterfall() works is that it passes on any arguments passed to the callback to the next function in the function list. 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. 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:

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

to:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742415972a4439752.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信