"No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?" 使用 js
"No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?" using js
这是生成错误的代码,调用此 Intent 时生成的实现响应是 "No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?"我应该如何更正它?
app.intent('getCrypto', (conv,{crypto="ethereum",cryptoactions="price"}) =>{
fetch('https://api.coinmarketcap.com/v1/ticker/').then(response => {
return response.json();
}).then(data => {
for (let i = 0; i < data.length - 1; i++) {
if (data[i].id === "bitcoin")
conv.data.price=data[i].price_usd;
conv.ask(`${conv.data.price} is the current value of ${crypto}`);
return response.json();
}
}).catch(err => {
return conv.ask(`${cryptoactions} of ${crypto} is not available. Would you like to know about another one?`);
});
});
错误消息描述了确切的问题"Is this being used in an async call that was not returned as a promise to the intent handler?"
您应该 return 来自意图处理程序的异步调用。因此,在调用 fetch
之前添加 return
应该可以解决问题。
中避免此问题
你必须return Promise
,你愿意使用这些结构吗?
app.intent('getCrypto', conv => {
// I - MUST HAVE PROMISE IN HERE
return new Promise(function (resolve, reject) {
fetch('https://api.coinmarketcap.com/v1/ticker/')
.then(res => {
...
resolve();
})
.catch(error => {
console.log(error);
reject(error)
});
})
.then(function (result) {
console.log(result);
// II - MUST HAVE THIS RESPONSE
// conv.ask(new SimpleResponse("..."));
conv.close(new SimpleResponse(texts.goodbye));
}, function (error) {
});
})
这是生成错误的代码,调用此 Intent 时生成的实现响应是 "No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?"我应该如何更正它?
app.intent('getCrypto', (conv,{crypto="ethereum",cryptoactions="price"}) =>{
fetch('https://api.coinmarketcap.com/v1/ticker/').then(response => {
return response.json();
}).then(data => {
for (let i = 0; i < data.length - 1; i++) {
if (data[i].id === "bitcoin")
conv.data.price=data[i].price_usd;
conv.ask(`${conv.data.price} is the current value of ${crypto}`);
return response.json();
}
}).catch(err => {
return conv.ask(`${cryptoactions} of ${crypto} is not available. Would you like to know about another one?`);
});
});
错误消息描述了确切的问题"Is this being used in an async call that was not returned as a promise to the intent handler?"
您应该 return 来自意图处理程序的异步调用。因此,在调用 fetch
之前添加 return
应该可以解决问题。
你必须return Promise
,你愿意使用这些结构吗?
app.intent('getCrypto', conv => {
// I - MUST HAVE PROMISE IN HERE
return new Promise(function (resolve, reject) {
fetch('https://api.coinmarketcap.com/v1/ticker/')
.then(res => {
...
resolve();
})
.catch(error => {
console.log(error);
reject(error)
});
})
.then(function (result) {
console.log(result);
// II - MUST HAVE THIS RESPONSE
// conv.ask(new SimpleResponse("..."));
conv.close(new SimpleResponse(texts.goodbye));
}, function (error) {
});
})