Alexa-skills-kit-sdk-for-nodejs: this.emit is undefined

Created on 22 Feb 2018  路  4Comments  路  Source: alexa/alexa-skills-kit-sdk-for-nodejs

Hi,
I have an issue within an intent where this.emit is undefined, pretty sure it's a scope issue because it happens in a callback, however, I cannot quite get the hang of it. The intent is defined as follows:
`
"WantCoffeeIntent": function () {

cloud = new TelldusAPI.TelldusAPI({ publicKey  : publicKey
                                      , privateKey : privateKey }).login(token, tokenSecret, function(err, user) { 
  // do some stuff
 this.emit(':tell',speechOutput);

}`
Any help or pointers would be much appreciated!

Most helpful comment

Hey @lostvicking - this is due to the function context changing in your callback function. The work-around that @benedekh points out above will save a reference to your handler's function context in a variable that

then you can call that.emit();

function handler() {
    const that = this;
    cloud = new TelldusAPI.TelldusAPI({ publicKey  : publicKey, privateKey : privateKey })
                         .login(token, tokenSecret, function(err, user) { 
        // your function context has changed here as you've declared a new function
        // that !== this
        // that.emit() will work
    }
}

Another option is to use an arrow function in your callback. This will preserve the parent function context (in this case, the function context bound around the handler() function)

function handler() {
    cloud = new TelldusAPI.TelldusAPI({ publicKey  : publicKey, privateKey : privateKey })
                         .login(token, tokenSecret, (err, user) => { 
          this.emit(':tell', speechOutput)
    }
}

All 4 comments

Save the reference for this in a context, where this.emit(...) is a valid function call. Then use this reference in the callback.

E.g.:

function handler(...){
var that = this;
....
cloud = new TelldusAPI....(...., function(err, user){
that.emit(':tell', speechOutput);
}

Hey @lostvicking - this is due to the function context changing in your callback function. The work-around that @benedekh points out above will save a reference to your handler's function context in a variable that

then you can call that.emit();

function handler() {
    const that = this;
    cloud = new TelldusAPI.TelldusAPI({ publicKey  : publicKey, privateKey : privateKey })
                         .login(token, tokenSecret, function(err, user) { 
        // your function context has changed here as you've declared a new function
        // that !== this
        // that.emit() will work
    }
}

Another option is to use an arrow function in your callback. This will preserve the parent function context (in this case, the function context bound around the handler() function)

function handler() {
    cloud = new TelldusAPI.TelldusAPI({ publicKey  : publicKey, privateKey : privateKey })
                         .login(token, tokenSecret, (err, user) => { 
          this.emit(':tell', speechOutput)
    }
}

I've had some trouble with this too - specifically related to state management. I can create that, which references this and get that to emit a response, but if i try to access/save more items to that.attributes I get a can't access attributes of undefined type of error. So maybe this is specific to the dynamodb integration?

Closing this issue for now. Please feel free to reopen it if you encounter further issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

captnCC picture captnCC  路  6Comments

mehtanilay10 picture mehtanilay10  路  6Comments

talkingnews picture talkingnews  路  5Comments

slesem picture slesem  路  4Comments

kevinwu picture kevinwu  路  3Comments