-->

IBM Action not returning anything after GET reques

2019-08-24 12:06发布

问题:

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.

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.

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};  
}

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.

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

Any ideas? In any case, thanks in advance.

回答1:

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);
         });
     });
  });
}