Abe Miessler
Abe Miessler

Reputation: 85126

"object is not a function" error?

I have a node.js application that is using async to call methods one after the other. For some reason when I try to go to the second tier of the waterfall, my application throws the following error:

TypeError: object is not a function

I have the following code which is coffeescript (i can get javascript if anyone wants it):

async.waterfall([
    (callback) =>
        console.log 'call getSurveyTitle'
        @getSurveyTitle (surveyTitle) =>
            fileName = 'Results_' + surveyTitle + '_' + dateString + '.csv'
            filePath = path.resolve process.env.PWD, 'static', @tempDir, fileName
            csv.to(fs.createWriteStream(filePath))
            callback(null, null)
    ,
    (callback) =>
        @createHeaderRow (headerRow) =>
            headerText = _.map headerRow, (row) =>
                row.text
            csv.write headerText
            console.log 'before' #gets here and then throws error
            callback(null,headerRow)
    ,
    (headerRow, callback) =>
        console.log 'after'
        @createRows headerRow, (callback) =>
            callback(null,null)
    ], (err, result) =>
        console.log "waterfall done!"
    )

Im fairly new to node and async, so I have a feeling I am just overlooking something obvious. Can anyone see what I am doing that could cause this error?

Upvotes: 0

Views: 1581

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123563

For waterfall, any callback arguments after the 1st error will be passed to the next task, including nulls:

async.waterfall([
    (callback) =>
        callback(null, null, null)
    ,
    (callback) =>
        console.log callback  # null
        console.log arguments # { 0: null, 1: null, 2: [Function], length: 3 }
])

If you don't want any values passed to the next task, just call callback with only the error argument:

callback(null)

Or set a name for the 1st argument so callback is 2nd:

(_, callback) =>
    @createHeaderRow (headerRow) =>
        # ...

Upvotes: 4

Related Questions