我正在尝试创建自定义的Alexa技能,在其中调用API。但是以某种方式我的代码表现得很奇怪。

情况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);
},


在这种情况下,cloudwatch中不会显示任何错误,但是即使执行this.emit()函数,该控件也不会获取getJSON函数。

以下是cloudwatch日志:

    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


情况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);
},


在这种情况下,即使日志中没有错误并且控件将使用getJSON,但是alexa表示“所请求的技能的响应存在问题”。

    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


我无法解决此问题。
我认为,我在getJSON()的回调中犯了一些错误。

最佳答案

看起来getJSON是一个异步函数:意味着它立即返回,并在准备好结果或引发错误时回调您。

因此,在您的示例中,调用了getJSON,但是它立即返回,随后您调用this.emit(':tell'),结束了处理程序,并在getJSON有机会完成并调用您的匿名函数之前,将处置发送回Alexa。打回来。

要解决此问题,请将this.emit(...)移到要传递给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!');
});

关于node.js - 自定义Alexa技能中NodeJS回调中的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49631981/

10-16 19:41