0

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事件的回调中运行它不起作用。

有人可以建议如何在我的代码中修复这个错误吗?

注意:您可以在https://github.com/jakhu/retis-ci的文件 src/downloader.coffee 中找到所有代码。随意查看其他文件,以防此错误与它们有关。

4

1 回答 1

0

在 unpacker.coffee 文件的第 56 行中,您正在使用比预期少一个参数调用 get 方法:

get(data.url, file_save, @download_options, (err) ->

正确的方法签名如下所示:

get(url, save, options, logger_options, callback)

这 - 当然 - 导致最后一个参数(回调)未定义。

于 2016-05-09T11:59:18.570 回答