【问题标题】:How to display API data using fetch on Dialogflow GUI如何在 Dialogflow GUI 上使用 fetch 显示 API 数据
【发布时间】:2021-06-17 18:43:02
【问题描述】:

我想在 Dialogflow GUI 上显示来自 OMDB API 的数据,但它没有发生。数据在 Google Cloud Console 上显示良好。

function infoHandler(agent){
    let movieName = agent.parameters.movie;
    agent.add(`The information for ${movieName} is as follow`);
    fetch('http://www.omdbapi.com/?apikey=e255decd%20&s='+ movieName)
    .then(result => result.json())
    .then((json) => {
        let id = json.Search[0].imdbID;
        fetch('http://www.omdbapi.com/?apikey=e255decd%20&i=' + id)
            .then(result => result.json())
            .then((json) => {
                agent.add(json.Title + json.Plot + json.imdbRatinng);
          return;
            }).catch((ex) => {
                console.log(ex);
            });
    })
    .catch((e) => {console.log(e);});
          

【问题讨论】:

    标签: fetch-api dialogflow-es-fulfillment


    【解决方案1】:

    问题是fetch() 导致异步操作。但是,没有任何东西向 Dialogflow 处理程序调度程序表明它是异步的,并且它应该等待它完成,然后再发回回复。为此,您需要返回一个 Promise。

    幸运的是,您拥有的 then/catch 链是基于 fetch() 构建的,它返回了一个 Promise。因此,您需要做的就是返回他们拥有的 Promise。在您的情况下,这很简单,只需在 fetch() 调用之前放置 return。所以它看起来像这样:

        return fetch('http://www.omdbapi.com/?apikey=e255decd%20&s='+ movieName)
          // Other lines remain the same
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-02
      • 2021-10-24
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 2021-09-12
      • 2023-04-09
      • 2021-04-18
      相关资源
      最近更新 更多