【发布时间】:2019-05-24 16:11:38
【问题描述】:
我在nodejs中有两个https请求,第一个是确认用户被授权访问数据,第二个是返回数据。
我有控制台日志,我看到数据已成功返回到承诺中,但 Alexa 不会说话/显示卡片。 cloudwatch 中没有错误。
我不完全理解 Promise 语法,所以我确定我遗漏了一些简单的东西。
我一直在更改语法,尝试 async/await,但似乎没有任何效果。
编辑的代码 - 在一些帮助下,我能够更好地布置我的代码。我现在在云手表中收到错误:错误:INVALID_RESPONSE,向技能分派请求时发生异常。
注意:出现此错误是因为我现在正在强制执行错误,也就是顶部(如果 redacted.errors)。
messages.NO_ACCESS 当前设置为 ----“嗯,您似乎无权访问该数据。”
const IntentRequest = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest';
},
handle(handlerInput) {
const { requestEnvelope, serviceClientFactory, responseBuilder } = handlerInput;
let resp, resultData;
return new Promise((resolve, reject) => {
checkAuthenticationStatus(handlerInput, function(json){
if(REDACTED.errors) {
console.log('unauthed', REDACTED.error)
reject(handlerInput.responseBuilder.speak(messages.NO_ACCESS).withSimpleCard('Unauthorized Request', messages.NO_ACCESS).getResponse());
} else if(REDACTED.noerror && REDACTED.noerror.data == 'true'){
const url = new URL(REDACTED.url);
const resp = httpsGetIntent(handlerInput, url, (theResult) => {
resultData = theResult;
console.log('resolved with ---->', d)
return resolve(handlerInput.responseBuilder.speak("The result was" + d).withSimpleCard('Hello World', d).getResponse());
})
}
})
});
},
};
这是返回数据的部分代码(resultData 和 d 是相同的东西,都返回数据),但她不说话/出示卡片:
const IntentRequest = {
canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest';
},
handle(handlerInput) {
const { requestEnvelope, serviceClientFactory, responseBuilder } = handlerInput;
let resp, resultData;
return new Promise((resolve, reject) => {
checkAuthenticationStatus(handlerInput, function(json){
if(REDACTED) {
reject(handlerInput.responseBuilder.speak(messages.NO_ACCESS).withSimpleCard('Unauthorized Request', messages.NO_ACCESS).getResponse())
} else if(REDACTED && REDACTED == 'true'){
const url = new URL(REDACTED);
resp = httpsGetIntent(handlerInput, url, (theResult) => {
resultData = theResult;
console.log(resultData)
}).then((d) => {
console.log(d)
resolve(handlerInput.responseBuilder.speak("The result was" + d.response.outputSpeech.text).withSimpleCard('Hello World', d.response.outputSpeech.text).getResponse());
}).catch((err) => { console.log(err)});
}
});
});
},
};
【问题讨论】:
标签: node.js lambda es6-promise alexa-skills-kit