GET请求后,IBM Action不返回任何内容 [英] IBM Action not returning anything after GET request

查看:102
本文介绍了GET请求后,IBM Action不返回任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是Java语言的初学者,所以如果我的信息有任何不确定性或不确定性,请随时纠正我.

First of all, I am a beginner in Javascript, so if there are any uncertainities or unclarities in my message, please feel free to correct me.

我尝试创建一个操作来支持我的IBM Watson Assistant.调用后,该操作应从http中获取一些信息并给出一些答案.

I try to create an action to support my IBM Watson Assistant. Once called, the action should get some info from a http and give some answer back.

获取"操作是Webhook的一部分,该Webhook通过Heroku成功部署为对Dialogflow的支持.我只是对其进行了一些更改,以使答案更容易.

The "get" action was part of a Webhook, successfully deployed via Heroku as support for Dialogflow. I just changed it a little bit, to make the answer easier.

function main(req){

    const http = require('http');
    const API_KEY = '85324cac';
    const prodname = req.prodname;
    const reqUrl = encodeURI(`http://www.omdbapi.com/?t=${prodname}&apikey=${API_KEY}`);

    http.get(reqUrl, (responseFromAPI) => {
        let completeResponse = '';

        responseFromAPI.on('data', (chunk) => {
            completeResponse += chunk;
        });

        responseFromAPI.on('end', () => {
            const movie = JSON.parse(completeResponse);
            let dataToSend = prodname ;
            dataToSend += (typeof movie.Title === "undefined") ? `Sorry the film is not in our database` : `${movie.Title} is a ${movie.Actors} starer ${movie.Genre} movie, released in ${movie.Year}. It was directed by ${movie.Director}.`;

            return {answer: dataToSend};
        });
    });
//return {answer: dataToSend};  
}

我期望返回"操作后有一个答案,但是它只显示空值.我很确定该操作永远不会进入"http.get"部分.当我删除////并调用代码时,它将返回以下消息:未定义dataToSend";如果我按原样保留代码(带有注释),则不会弹出错误.

I was expecting an answer after the "return" action, but it is only showing empty values. I am pretty sure that the action does never get into the "http.get" part. When I remove the // and I invoke the code, it returns the following message: "dataToSend is not defined"; if I keep code as it is (with the comment), no errors pop up.

omdapi是免费的,但在美国托管,以防万一.

The omdapi is for free, but hosted in the US, in case that could matter.

有什么想法吗?无论如何,请先谢谢.

Any ideas? In any case, thanks in advance.

推荐答案

认为您会发现在返回对omdbapi的外部调用之前,您的ibm函数已完成.您最好的选择是使用Promise(是全新的,我希望您可能还没有使用Promise-建议阅读

Think you will find that your ibm function is completing before your external call to omdbapi is returning. Your best choice here is to use promises ( being new I expect you may not have used promises yet - would recommend reading https://cloud.ibm.com/docs/openwhisk?topic=cloud-functions-creating-javascript-actions#creating-javascript-actions
Not your complete program, leave you something to play with;

function main(req){
  const http = require('http');
  const API_KEY = '85324ca';
//const prodname = req.prodname;
  const prodname = 'Game%20of%20Thrones&Season=1';
  const reqUrl = 'http://www.omdbapi.com/?t=Game%20of%20Thrones&Season=1&apikey=85324cac';
//const reqUrl = encodeURI(`http://www.omdbapi.com/?t=${prodname}&apikey=${API_KEY}`);

return new Promise(function(resolve, reject) {
       http.get(reqUrl, (responseFromAPI) => {
           let completeResponse = '';
           responseFromAPI.on('data', (chunk) => {
               completeResponse += chunk;
               // you could return answer here via resolve.
               //var parsedData = JSON.parse(completeResponse);
               //console.log(parsedData);
               //resolve(parsedData);
           })
           responseFromAPI.on('error', (error) => {
               console.log(error);
               reject(error);
           })
           responseFromAPI.on('end', () => {
               var parsedData = JSON.parse(completeResponse);
               console.log(parsedData);
               resolve(parsedData);
         });
     });
  });
}

这篇关于GET请求后,IBM Action不返回任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆