首页 文章

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

提问于
浏览
1

我正在尝试创建一个自定义的Alexa技能,我在其中调用API . 但不知何故,我的代码表现得很奇怪 .

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

在这种情况下,cloudwatch中没有显示错误,但控件不会执行 getJSON 函数,但会执行 this.emit() 函数 .

以下是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

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

在这种情况下,即使日志中没有错误,控件也将获得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()的回调中犯了一些错误 .

2 回答

  • 1

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

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

    要解决,请在回传函数中移动 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/

相关问题