简体   繁体   中英

Uncaught ReferenceError when trying to change js function into coffeescript

This is the error:

Uncaught ReferenceError: er is not defined

This is the code ..

{CompositeDisposable} = require 'atom'
lib = require 'lib'

module.exports =
  subscriptions: null

  activate: ->
    @subscriptions = new CompositeDisposable
    @subscriptions.add atom.commands.add 'atom-workspace',
      'my-package:convert': => @convert()

  deactivate: ->
    @subscriptions.dispose()

  convert: ->
    console.log 'Convert text!'
    if editor = atom.workspace.getActiveTextEditor()
      console.log editor.getText()
      lib.process(editor.getText() (er files)) ->
        console.log 'All files ..'
        listFiles(files)

  listFiles: (files) ->
    for fileName in Object.keys(files)
      first = files[fileName].first
      second = files[fileName].second
      third = files[fileName].third
      console.log 'Done'

This is the javascript I'm trying to change to coffeescript inside convert() ..

lib.process(text, function (er, files) {
  console.log('All files ..');
  listFiles(files);
});

The line

lib.process(editor.getText() (er files)) ->

is not correct, since it's missing the commas needed to separate the parameters.

When trying to transpile the line you wrote to JS it would look like this:

lib.process(editor.getText()(er(files)))(function() {});

When trying to migrate JS to coffee, it's sometimes a good idea to start with http://js2.coffee/ and start from there. Always look at the transpiled JS result in order to check if everything will work as expected

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