我有一个使用async的node.js应用程序一个接一个地调用方法。由于某种原因,当我尝试进入 waterfall 的第二层时,我的应用程序抛出以下错误:
TypeError: object is not a function
我有以下代码,即coffescript(如果有人需要,我可以获取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!"
)
我对 Node 和异步相当陌生,所以我有一种感觉,我只是忽略了一些明显的东西。谁能看到我正在做的事情可能会导致此错误?
对于waterfall
,第一个error
之后的任何callback
参数都将传递到下一个任务,包括null
:
async.waterfall([
(callback) =>
callback(null, null, null)
,
(callback) =>
console.log callback # null
console.log arguments # { 0: null, 1: null, 2: [Function], length: 3 }
])
如果您不希望将任何值传递给下一个任务,只需使用error
参数调用callback
即可:
callback(null)
或者为第一个参数设置一个名称,这样回调
就是第二个:
(_, callback) =>
@createHeaderRow (headerRow) =>
# ...
我是一名优秀的程序员,十分优秀!