错误:未设置响应.这是否用于未作为对意图处理程序的承诺返回的异步调用中?



我有一个返回承诺的函数

function superHero_with_name (name) {
return new Promise((resolve, reject) => {
var encoded_superhero_name = name.split(" ").join("%20");
var path_superhero = '/api.php/' + superhero_access_token + '/search/' + encoded_superhero_name ;
console.log('API Request: ' + host_superhero + path_superhero);
http.get({host: host_superhero, path: path_superhero}, (res) => {
let body = '';
res.on('data', (d) => { body += d;});
res.on('end', () => {
// console.log("BODY:", body);
let output = JSON.parse(body);
resolve(output);
});
res.on('error', (error) => {
console.log(`Error while calling the API ${error}` );
});
});
});
}

我正在使用操作 SDK,调用此函数后抛出的错误是

未设置响应。这是否用于未作为对意图处理程序的承诺返回的异步调用中?

我调用这个函数的函数是

superhero.intent('superherocard', (conv, {superhero_entity}) => {
superHero_with_name(superhero_entity).then((output)=>{
if(output.response === 'success'){
var super_name = output.results[0].name ;
// Assigned values to all the variables used below this comment
conv.ask("Here's your Superhero");
conv.ask(new BasicCard({
text: `Your SuperHero is the mighty **${super_name}**.  n**INTELLIGENCE** *${intelligence}*  n**STRENGTH** *${strength}*  n**SPEED** *${speed}*  n**DURABILITY** *${durability}*  n**POWER** *${power}*  n**COMBAT** *${combat}*`,
title: super_name,
subtitle: super_publisher,
image: new Image({
url: image_superhero,
alt: super_name,
}),
}));
conv.ask(text_1);
conv.ask(new Suggestions(itemSuggestions));
} else{
conv.ask('Sorry, I cannot find the superhero you are finding! But you can become one by helping and motivating people.');
}
return console.log("superHeroCard executed");
}).catch(()=> {
conv.ask('Sorry, I cannot find the superhero you are finding! But you can become one by helping and motivating people.');
});
});

我无法找到错误,因为我正在返回承诺,但意图处理程序无法读取它。

问题是意图处理程序正在读取它,但它没有返回它。

如果您正在执行异步调用,则处理程序必须返回一个 Promise,该 Promise 将在异步部分完成后解析。如果您没有执行异步调用,则无需返回承诺 - 因为处理程序调度程序在发送回回复之前不需要等待任何内容。如果您正在执行异步调用,承诺会向调度程序指示它需要等待承诺完全解析,然后才能返回您设置的响应。

在您的情况下,您可能可以调整处理程序的前两行以返回函数调用和then()链返回的 Promise。可能是这样的:

superhero.intent('superherocard', (conv, {superhero_entity}) => {
return superHero_with_name(superhero_entity).then((output)=>{
// ...

最新更新