【发布时间】:2017-02-26 14:37:23
【问题描述】:
我正在开发在 AWS Lambda 中运行的 Node 中的 Alexa 技能,并且在我发出事件时无法执行回调。 Node.JS README 的 Alexa-skills-kit 演示了将回调函数传递给事件处理程序,并建议使用箭头函数来保留上下文:
this.emit('JustRight', () => {
this.emit(':ask', guessNum.toString() + 'is correct! Would you like to play a new game?',
'Say yes to start a new game, or no to end the game.');
});
我正在尝试做同样的事情,但发现我的回调似乎从未被执行。我认为这是因为 AWS Lambda 仅限于 Node 4.3.2,并且箭头函数不可用,所以我尝试将这个上下文以老式方式传递回回调:
在新会话处理程序中:
if(!account_id) {
console.log('Access token:' + accessToken);
this.emit('getAccount', accessToken, function (retrieved_id) {
console.log('account id in callback: ' + retrieved_id);
this.emit('welcome');
});
}
在事件处理程序中:
accountHandler = {
'getAccount': function (accessToken, cb) {
console.log('fetching account id');
var client = thirdparty.getClient(accessToken);
var r = client.getAccountForToken(client);
r.then(function (data) {
console.log('got it:' + data);
this.attributes['account_id'] = data;
cb.call(this, data);
}).catch(function (err) {
this.emit('handleApiError', err);
});
},
}
我可以在日志中看到我已成功检索账户 ID,但 Lambda 正在执行且没有错误且没有调用我的回调函数。我试图弄清楚在 Promise 'then' 函数中调用回调是否有问题,或者是否发生了其他事情。
【问题讨论】:
标签: node.js aws-lambda alexa-skills-kit alexa-skill