最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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)
}
发布评论

评论列表(0)

  1. 暂无评论