我试图在咖啡脚本中的流触发end
事件时运行回调。在尝试运行我的代码时,我收到以下错误:
C:\Users\Gum-Joe\Documents\Projects\retisci\lib\downloader.js:45
callback();
^
TypeError: callback is not a function
at Request.<anonymous> (C:\Users\Gum-Joe\Documents\Projects\retisci\lib\downloader.js:45:5)
at emitOne (events.js:101:20)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (C:\Users\Gum-Joe\Documents\Projects\retisci\node_modules\request\request.js:988:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:926:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
注意:输出已修改以保护我的全名。
这是我的代码:
###
# Downloader
# @param url {String} url to GET
# @param save {String} Save file
# @param options {Object} Options
# @param callback {Function} Callback
###
downloader.get = (url, save, options, logger_options, callback) ->
log = require('single-line-log').stdout
# Download
# Logger
logger = new Logger('retis', logger_options)
# File stream
file_stream = fs.createWriteStream(save)
log("#{"[retis INFO]".green} Downloading #{url}...0% at 0 kb/sec...\n")
progress(request(url))
.on('progress', (state) ->
percent = "#{Math.floor(state.percentage * 100)}% [#{Math.round(state.size.transferred / 1024)} kb of #{Math.round(state.size.total / 1024)} kb]"
log("#{"[retis INFO]".green} Downloading #{url}...#{percent} at #{Math.round(state.speed / 1024)} kb/sec...\n")
)
.on('data', (d) ->
file_stream.write d
return
)
.on 'error', (e) ->
callback(e)
return
.on 'end', () ->
log("#{"[retis INFO]".green} Downloading #{url}...100%\n")
logger.info("Downloaded #{url}.\n")
# This is what causes the error
callback()
return
return
在函数开始时运行回调,在函数结束时运行它,但在end
事件的回调中运行它不会。
有人可以建议如何在我的代码中修复此错误吗?
NB:您可以在文件src / downloader.coffee中的https://github.com/jakhu/retis-ci找到所有代码。如果这个bug与它们有关,请随意查看其他文件。
答案 0 :(得分:0)
在unpacker.coffee文件的第56行中,您调用get方法,其中一个param少于预期:
get(data.url, file_save, @download_options, (err) ->
正确的方法签名如下所示:
get(url, save, options, logger_options, callback)
当然 - 这导致最后一个参数(回调)未定义。