znat
znat

Reputation: 13474

TypeError: object is not a function with async.waterfall

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

Upvotes: 2

Views: 5386

Answers (1)

mscdex
mscdex

Reputation: 106698

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

Upvotes: 7

Related Questions