我必须在这里遗漏一些非常简单的东西,但是这里有。
我刚刚开始学习 Alexa 开发,并发现了 alexa-app 模块,它似乎使 Alexa 编程非常简单——除了我遇到的这个网络问题。
我正在浏览名为 AirportInfo 的团队提供的示例应用程序。问题区域的代码如下:
var faaHelper = new FAADataHelper();
faaHelper.requestAirportStatus(airportCode).then(function(airportStatus) {
var goodMessage = faaHelper.formatAirportStatus(airportStatus);
console.log(goodMessage);
res.say(goodMessage).send();
}).catch(function(err) {
console.log(err.statusCode);
var prompt = 'I didn\'t have data for an airport code of ' + airportCode;
console.log(prompt);
res.say(prompt).reprompt(reprompt).shouldEndSession(false).send();
});
return false;
依次调用这些函数:
FAADataHelper.prototype.requestAirportStatus = function(airportCode) {
return this.getAirportStatus(airportCode).then(
function(response) {
console.log('success - received airport info for ' + airportCode);
return response.body;
}
);
};
FAADataHelper.prototype.getAirportStatus = function(airportCode) {
var options = {
method: 'GET',
uri: ENDPOINT + airportCode,
resolveWithFullResponse: true,
json: true
};
return rp(options);
};
这对我来说看起来不错,但是当代码运行时,控制何时将响应发送回设备的主要 Alexa 应用“请求”比预期的要早返回。不是带有所选机场预期天气信息的完整响应负载,而是在return rp(options)
呼叫后立即发回响应。.then()
第一个代码示例中的块中执行的代码在技能已经将相当于空响应的内容发送回 Alexa 之后运行。这实际上使 Alexa 崩溃,因为她说出了某种关于技能错误的神秘信息。
这是我的 server.js 代码:
var AlexaAppServer = require("../index.js");
AlexaAppServer.start({
server_root: './',
port: 8080,
debug: true,
// Use preRequest to load user data on each request and add it to the request json.
// In reality, this data would come from a db or files, etc.
preRequest: function(json, req, res) {
console.log("preRequest fired");
json.userDetails = { "name": "Bob Smith" };
},
// Add a dummy attribute to the response
postRequest: function(json, req, res) {
// look for this output in the log below
console.log("postRequest fired");
json.dummy = "text";
}
});
这是显示我正在描述的这种情况的调试日志:
preRequest fired
REQUEST { method: 'GET',
uri: 'http://services.faa.gov/airport/status/dfw',
resolveWithFullResponse: true,
json: true,
simple: false,
callback: [Function: RP$callback],
transform: undefined,
transform2xxOnly: false }
postRequest fired
REQUEST make request http://services.faa.gov/airport/status/dfw
REQUEST onRequestResponse http://services.faa.gov/airport/status/dfw 200 { date: 'Fri, 24 Mar 2017 05:09:41 GMT',
server: 'Apache',
...
'access-control-allow-origin': '*',
'access-control-allow-methods': 'GET, HEAD, OPTIONS',
'version-requested': 'Any',
connection: 'close',
'transfer-encoding': 'chunked',
'content-type': 'application/json;charset=UTF-8' }
REQUEST reading response's body
...
REQUEST end event http://services.faa.gov/airport/status/dfw
REQUEST has body http://services.faa.gov/airport/status/dfw 517
REQUEST emitting complete http://services.faa.gov/airport/status/dfw
success - received airport info for dfw
注意相对于 的位置preRequest Fired
和postRequest Fired
显示success - received airport info for dfw
。任何想法我做错了什么?在我的 Node 环境中配置错误,或者可能是错误的依赖版本?我很怀疑,因为它在我的调试器(VS Code)中从节点命令提示符和从 Lambda 中失败。