IBM Action 在 GET 请求后不返回任何内容
IBM Action not returning anything after GET request
首先,我是Javascript的初学者,所以如果我的消息中有任何不确定或不清楚的地方,请随时指正。
我尝试创建一个操作来支持我的 IBM Watson Assistant。调用后,操作应该从 http 获取一些信息并返回一些答案。
"get" 操作是 Webhook 的一部分,已通过 Heroku 成功部署以支持 Dialogflow。我只是稍微改变了一下,让答案更容易。
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};
}
我期待在 "return" 操作后得到答复,但它只显示空值。我很确定该操作永远不会进入 "http.get" 部分。当我删除 // 并调用代码时,它 returns 显示以下消息:"dataToSend is not defined";如果我保持代码原样(带有注释),则不会弹出任何错误。
omdapi 是免费的,但托管在美国,以备不时之需。
有什么想法吗?无论如何,提前致谢。
认为您会发现在对 omdbapi 的外部调用返回之前,您的 ibm 函数正在完成。你最好的选择是使用 promises(作为新手我想你可能还没有使用过 promises - 推荐阅读 https://cloud.ibm.com/docs/openwhisk?topic=cloud-functions-creating-javascript-actions#creating-javascript-actions
不是你的完整程序,留给你玩玩;
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);
});
});
});
}
首先,我是Javascript的初学者,所以如果我的消息中有任何不确定或不清楚的地方,请随时指正。
我尝试创建一个操作来支持我的 IBM Watson Assistant。调用后,操作应该从 http 获取一些信息并返回一些答案。
"get" 操作是 Webhook 的一部分,已通过 Heroku 成功部署以支持 Dialogflow。我只是稍微改变了一下,让答案更容易。
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};
}
我期待在 "return" 操作后得到答复,但它只显示空值。我很确定该操作永远不会进入 "http.get" 部分。当我删除 // 并调用代码时,它 returns 显示以下消息:"dataToSend is not defined";如果我保持代码原样(带有注释),则不会弹出任何错误。
omdapi 是免费的,但托管在美国,以备不时之需。
有什么想法吗?无论如何,提前致谢。
认为您会发现在对 omdbapi 的外部调用返回之前,您的 ibm 函数正在完成。你最好的选择是使用 promises(作为新手我想你可能还没有使用过 promises - 推荐阅读 https://cloud.ibm.com/docs/openwhisk?topic=cloud-functions-creating-javascript-actions#creating-javascript-actions
不是你的完整程序,留给你玩玩;
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);
});
});
});
}