alexa / alexa-skills-kit-sdk-for-nodejs

The Alexa Skills Kit SDK for Node.js helps you get a skill up and running quickly, letting you focus on skill logic instead of boilerplate code.
Apache License 2.0
3.12k stars 736 forks source link

alexa skills kit sdk not allowing AWS IoT Shadow updates? #112

Closed bryanyuchen closed 7 years ago

bryanyuchen commented 7 years ago

It seems that I'm not able to update my AWS IoT Shadows while using this sdk.

Regarding the case with handlers, I've found that the following is valid code, but fails to update the shadow

var handlers = {

'HelloWorldIntent': function () {
    <update shadow function using iotData.updateThingShadow>
    this.emit(':tell', 'Hello World!');
}

};

Not exactly sure why this is the case, but I found that when I comment out the this.emit line, the function is actually able to update the shadow, but then there is no alexa response. Any help would be appreciated. Thanks!

knowlsie commented 7 years ago

I had a very similar issue just a few days ago. The problem is that when you emit, the SDK calls context.succeed() which ends the Lambda execution, ignoring whatever is left in the Node event loop. To solve this, you need to make your emit clause a callback of your updating code - i.e. make it so it only emits after it's finished updating the IoT shadows.

I have no experience with IoT shadows, but let me know if you need any further help. (Interestingly enough, your code should work when https://github.com/alexa/alexa-skills-kit-sdk-for-nodejs/pull/59 is merged so it calls callback() not context.succeed(), but that hasn't happened yet.)

bryanyuchen commented 7 years ago

Hi Tom,

Thank you so much for your helpful comments! I understand the situation much better now and fixed the issue by passing a callback to this.emit:

updateShadow( () => { this.emit(':tell', speechOutput); } ); //where updateShadow only takes 1 argument

knowlsie commented 7 years ago

No worries, and that's an ideal fix! Your original code really should work, and will work as soon as they get round to properly updating that part of the SDK.

In case anyone else is looking at this issue and wondering whether this is why their async code isn't working, I'll try to leave a comment when they've updated it. Once it's updated you won't need to put the emit in a callback anymore.