我在nodejs中有两个https请求,第一个是确认用户有权访问数据,第二个是返回数据。
我在整个控制台中都有日志,我看到在诺言中成功返回了数据,但是Alexa不会说/不显示卡。 cloudwatch中没有错误。
我没有完全理解Promise语法,因此我确定我缺少一些简单的东西。
我一直在改变语法,尝试异步/等待,似乎没有任何作用。
编辑代码-在一些帮助下,我能够更好地布置代码。我现在在云监视中遇到错误:错误:INVALID_RESPONSE,将请求分派给技能时发生异常。
注意:发生此错误是因为现在我正在强制错误,也就是最上面的部分(如果redact.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());
})
}
})
});
},
};
这是代码的一部分,其中返回了数据IS(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)});
}
});
});
},
};
最佳答案
您需要考虑一些事情,但这个问题尚不清楚。
这里所说的是什么,该价值从何而来?您没有在任何地方提到它。
为了方便起见,我们假设它是一个全局变量。
如果REDACTED出于任何原因为false,则永远不会执行您的代码。因为条件永远不会满足。因此,这一承诺既不会拒绝也不会解决。
如果假设您在第一条if语句中错误地编写了REDACTED,则可能应该为!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);
});
这里的httpsGetIntent也不正确,您有一个回调方法,该方法接收TheResult并附加一个then方法,这没有任何意义。而且resultData在这里什么也没做。
假设checkAuthenticationStatus和httpGetIntent都具有回调模式,则可以编写如下内容
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);
httpsGetIntent(handlerInput, url, theResult => {
resultData = theResult;
console.log(resultData);
return resolve(
handlerInput.responseBuilder
.speak("The result was" + d.response.outputSpeech.text)
.withSimpleCard("Hello World", d.response.outputSpeech.text)
.getResponse()
);
})
}
});
});
}
};
关于node.js - 错误:INVALID_RESPONSE-数据正在从 promise 中返回,但无法让Alexa讲话/显示卡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56296212/