简体   繁体   中英

TypeError: callback is not a function when executing a callback

I'm trying to run a callback when the end event is fired by a stream in coffee-script. On attempting to run my code, I receive the following error:

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)

NB: Output modified to protect my full name.

Here is my code:

###
# 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

Running the callback at the start of the function works, running it at the end of the function works, but running it in the end event's callback doesn't.

Could someone please suggest how to fix this bug in my code?

NB: You can find all the code at https://github.com/jakhu/retis-ci in the file src/downloader.coffee. Feel free to look at the other files, in case this bug relates to them.

In line 56 of the unpacker.coffee file you are calling the get method with one param less than expected:

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

The correct method signature looking like this:

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

That - of course - leads to the last param (callback) to be undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM