Problems in nodejs callback in custom alexa skill

Akash Mahapatra

I am trying to create a custom Alexa skill in which I am calling an API. But somehow my code is behaving weird.

CASE - 1

'firstChance': function () {

    // Some code here//

    getJSON(options, function (err, result) {
        if (err) {
            return console.log('ERROR while trying to get country names', err);
        }

         console.log(result);
    });
    this.emit(':tell', speechOutput, speechOutput);
},

In this case no errors are shown in cloudwatch but the control is not going to getJSON function eventhough this.emit() function is executed.

Below are the cloudwatch logs:

    CloudWatch logs:

  
13:42:49
START RequestId: ************ Version: $LATEST


13:42:49
2018-04-03T13:42:49.578Z    *************** Warning: Application ID is not set


13:42:49
END RequestId: *********************


13:42:49
REPORT RequestId: **************    Duration: 74.03 ms  Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 48 MB


13:42:51
START RequestId: ***************** Version: $LATEST


13:42:51
2018-04-03T13:42:51.647Z    *********************** Warning: Application ID is not set


13:42:51
END RequestId: *************************


13:42:51
REPORT RequestId: ************************  Duration: 153.09 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 49 MB

CASE 2 :

'firstChance': function () {

    // Some code here//


    getJSON(options, function (err, result) {
        if (err) {
            return console.log('ERROR while trying to get country names', err);
        }

         console.log(result);
    });
    //this.emit(':tell', speechOutput, speechOutput);
},

In this case even though there are no errors in the logs and the control is going to getJSON, but alexa says "There was a problem with the requested skill's response".

    Below are the cloudwatch logs:


13:35:32
START RequestId: ************************** Version: $LATEST


13:35:32
2018-04-03T13:35:32.896Z    e16ddc70-3743-11e8-bf3b-a98fb0c89baf    Warning: Application ID is not set


13:35:32
END RequestId: **************************


13:35:32
REPORT RequestId: **************************    Duration: 110.81 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 48 MB


13:35:35
START RequestId: ************************** Version: $LATEST


13:35:35
2018-04-03T13:35:35.549Z    **************************  Warning: Application ID is not set


13:35:35
2018-04-03T13:35:35.861Z    **************************  Response from Server started


13:35:35
2018-04-03T13:35:35.861Z    **************************  Server Status: 200


13:35:35
2018-04-03T13:35:35.861Z    **************************  Response Headers : {"server":"Cowboy","connection":"close","x-powered-by":"Express","content-type":"application/json; charset=utf-8","content-length":"187238","etag":"W/\"Vhlms2jCBxpTPF7sp9mxAw==\"","vary":"Accept-Encoding","date":"Tue, 03 Apr 2018 13:35:35 GMT","via":"1.1 vegur"}


13:35:35
2018-04-03T13:35:35.978Z    **************************  Preparing the hash map...


13:35:36
2018-04-03T13:35:35.978Z    **************************  [ { name: { common: 'Afghanistan', official: 'Islamic Republic of Afghanistan', native: [Object] }, tld: [ '.af' ], cca2: 'AF', ccn3: '004', cca3: 'AFG', currency: [ 'AFN' ], callingCode: [ '93' ], capital: 'Kabul', altSpellings: [ 'AF', 'Afġānistān' ], relevance: '0', region:


13:35:36
END RequestId: **************************


13:35:36
REPORT RequestId: **************************    Duration: 1249.65 ms    Billed Duration: 1300 ms Memory Size: 128 MB    Max Memory Used: 57 MB


13:35:36
START RequestId: ************************** Version: $LATEST


13:35:36
2018-04-03T13:35:36.954Z    e46c4ff4-3743-11e8-a19e-036de9469172    Warning: Application ID is not set


13:35:36
END RequestId: **************************


13:35:36
REPORT RequestId: **************************    Duration: 1.97 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 57 MB

I am not able to resolve this. I think, I have made some mistake in the callback of getJSON().

Mike Dinescu

It looks like getJSON is an asynchronous function: meaning it returns immediately and it calls you back when the results are ready, or when an error is thrown.

So, in your example, getJSON is invoked, but it returns immediately, followed by your call to this.emit(':tell') which ends your handler and sends the respobse back to Alexa before getJSON has a chance to complete and call your anonymous function callback.

To solve, move this.emit(...) inside the callback function you are passing into getJSON

 getJSON(options, function (err, result) {
    if (err) {
        // handle the error
        this.emit(':tell', 'Error getting country names');
        return console.log('ERROR while trying to get country names', err);
    }

     console.log(result);
     // handle the successful result
     this.emit(':tell', 'The country name was retrieved!');
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related