2013-03-05 86 views
0

我有一个node.js应用程序使用async一个接一个地调用方法。出于某种原因,当我尝试去瀑布的第二层,我的应用程序将引发以下错误:“对象不是函数”错误?

TypeError: object is not a function

我有下面的代码是CoffeeScript的(如果有人想它,我可以得到的JavaScript):

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!" 
    ) 

对节点和异步我还是比较新的,所以我有一种感觉,我只是忽略了一些明显的东西。任何人都可以看到我在做什么,可能会导致此错误?

回答

4

对于waterfall,第一error后的任何callback参数将被传递到下一个任务,包括null S:

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

如果你不想传递到下一个任务的任何值,只需调用callback只用error说法:

callback(null) 

还是为第一个参数,以便callback设置一个名称为第二:

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

真棒谢谢你!那么发生的事情是,第二层是否收到一个空参数,什么时候它根本没有收到任何参数? – 2013-03-05 17:43:18

+0

@AbeMiessler那么,“*应*”取决于你。但是,是的,它收到了一个目前没有预料到的'空'参数。 – 2013-03-05 17:45:14

相关问题