我正在尝试创建一个自定义的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
案例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()的回调中犯了一些错误。
答案 0 :(得分:2)
看起来-Xbootclasspath/a:additional.jar
是一个异步函数:意味着它会立即返回,并在结果准备好或抛出错误时调用回来。
因此,在您的示例中,getJSON
被调用,但它立即返回,然后调用getJSON
,结束您的处理程序并在this.emit(':tell')
之前将respobse发送回Alexa有机会完成并调用您的匿名函数回调。
要解决,请将getJSON
移到您传递给this.emit(...)
的回调函数中
getJSON
答案 1 :(得分: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/