nodejs回调自定义alexa技能中的问题

问题描述 投票:1回答:2

我正在尝试创建一个自定义的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()的回调中犯了一些错误。

node.js aws-lambda alexa-skills-kit alexa-skill project-flogo
2个回答
2
投票

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

所以,在你的例子中,getJSON被调用,但它会立即返回,然后你调用this.emit(':tell'),它结束你的处理程序并在getJSON有机会完成并调用你的匿名函数回调之前将respobse发送回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!');
});

0
投票

试试这个,

    '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);
    });

},

或者你可以使用set timeout:

        '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);
        });

        setTimeout(() => {

             this.emit(':tell', speechOutput, speechOutput);
          }, 2500)       


    },

或者您可以尝试Async - Await,现在可以在AWS Lambda中本地使用,如https://aws.amazon.com/blogs/compute/node-js-8-10-runtime-now-available-in-aws-lambda/所述

© www.soinside.com 2019 - 2024. All rights reserved.