错误:未设置任何响应。是否在异步调用中使用了该调用,而该调用未作为对意图处理程序的承诺返回?

时间:2018-10-09 14:28:24

标签: dialogflow actions-on-google

我有一个返回 Promise

的函数
  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}` );
            });
        });
    });
  }

我正在使用Actions 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时找不到错误,但是意图处理程序无法读取它。

1 个答案:

答案 0 :(得分:2)

问题在于Intent Handler 正在读取它,但不是返回

如果您正在进行异步调用,则处理程序必须返回一个Promise,该承诺将在异步部分完成后解决。如果您不进行异步调用,则无需返回诺言-因为处理程序调度程序不需要等待任何内容就可以返回答复。如果您正在进行异步调用,则Promise会向调度程序指示,它需要等待Promise完全解决后才能返回您设置的响应。

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

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