简体   繁体   中英

CoffeeScript function as parameter in node.js API

I'm writing a plugin for Atom in CoffeeScript and I'm using this nodejs API (telegram.link) . It has some asymetric functions so I have to pass functions as parameters which get called as callback. My problem is that when I use the following code:

login: ->
    console.log 'Loging in'
    @client = telegramLink.createClient({
        id: config.telegram.prod.app.id,
        hash: config.telegram.prod.app.hash,
        version: '0.0.1',
        lang: 'en'
    },
    config.telegram.prod.primaryDataCenter);
    @client.createAuthKey(@authCallback)
    console.log @client

authCallback: (auth) ->
    console.log auth
    @client.auth.sendCode(config.telegram.test.telNr, 5, 'en', @sendCodeCallback)

which gets compiled to:

login: function() {
  console.log('Loging in');
  this.client = telegramLink.createClient({
    id: 12345,
    hash: 'q1w2e3r4t5y6u7i8o9p0',
    version: '0.0.1',
    lang: 'en'
  }, config.telegram.prod.primaryDataCenter);
  this.client.createAuthKey(this.authCallback);
  return console.log(this.client);
},
authCallback: function(auth) {
  console.log(auth);
  return this.client.auth.sendCode(config.telegram.test.telNr, 5, 'en', this.sendCodeCallback);
}

@client is undefined in the authCallback function.

I read on Stackoverflow ( CoffeeScripts classes - access to property in callback ) that I should use => (fat-arrow) so I tried this resulting in the following compiled script:

authCallback: (function(_this) {
  return function(auth) {
    console.log(auth);
    return _this.client.auth.sendCode(config.telegram.test.telNr, 5, 'en', _this.sendCodeCallback);
  };
})(this)

But @client remains undefined. I think that maybe the callback function call out of the API doesn't work properly anymore.

What else can I do to keep the original scope, but make it work with the API?

When I look at the indetion of your code, I would guess that you are not inside a class. If you are not inside a instance of a class "this" or "@" will not work. The second thing is that the the definition of the callback. It should be not defined as a method of your class. The class could look something like this:

class MyClass
  constructor: ->
    // do something
  login: ->
    @client = telegramLink.createClient({
        id: config.telegram.prod.app.id,
        hash: config.telegram.prod.app.hash,
        version: '0.0.1',
        lang: 'en'

     @client.authCallback: (auth) =>
       console.log auth
       @client.auth.sendCode(config.telegram.test.telNr, 5, 'en', @sendCodeCallback)

That your class thing works you need to create an instance so maybe that is not what you want.

You could change your code that it works like you defined it just with functions.

login: ->
    console.log 'Loging in'
    client = telegramLink.createClient({
        id: config.telegram.prod.app.id,
        hash: config.telegram.prod.app.hash,
        version: '0.0.1',
        lang: 'en'
    },
    config.telegram.prod.primaryDataCenter);
    client.createAuthKey(@authCallback)
    console.log @client

authCallback: (auth) ->
  console.log auth
  client.auth.sendCode(config.telegram.test.telNr, 5, 'en', sendCodeCallback)

Hint: the definition of your options dictionary inside the createClient method looks like JS and not like Coffescript. Also the ";" you should not use. To mix the definitions could also create some mistakes.

Solution:

Today I found my solution for the problem. Now my code looks like so:

login: ->
console.log 'Logging in'
@client =
  telegramLink.createClient({
    id: config.telegram.prod.app.id
    hash: config.telegram.prod.app.hash
    version: '0.0.1'
    lang: 'en'
  }
  config.telegram.prod.primaryDataCenter)
@client.createAuthKey((auth) =>
  console.log @client.auth
  @client.auth.sendCode(
    config.telegram.test.telNr,
    5,
    'en',
    @sendCodeCallback))
console.log @client

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