I am trying to display some data from an api withint the console, but it seems like I didn't understand how the code execution works in ES6.
getMovieReccomendation({context, entities}){
return new Promise(function (resolve, reject) {
var movie = firstEntityValue(entities, 'movie')
if ('movie') {
var movieData = omdbapicall(movie);
console.log(movieData);
context.movieReccomendation = 'You should see ' + movieData.Title;
delete context.missingMovieTitle;
} else {
context.missingMovieTitle = true;
delete context.movieReccomendation;
}
return resolve(context);
})
}
};
function omdbapicall(movieTitle) {
console.log(movieTitle);
request('http://www.omdbapi.com/?t=' + movieTitle + '&plot=short&r=json', function (error, response, body) {
if (error) {
throw error;
}
if (response.statusCode == 200) {
var apiData = JSON.parse(body);
var movieInfo = {
"Title": apiData.Title,
"Plot": apiData.Plot,
"imdbRating": apiData.imdbRating,
"imdbVotes": apiData.imdbVotes
};
console.log(apiData);
return apiData;
}
})
}
The Error I get is the following :
Memento
undefined
TypeError: Cannot read property 'Title' of undefined
at C:\Users\Derp\WebstormProjects\moviereccomendation\examples\quickstart.js:69:76
at Object.getMovieReccomendation (C:\Users\Derp\WebstormProjects\moviereccomendation\examples\quickstart.js:64:16)
at C:\Users\Derp\WebstormProjects\moviereccomendation\lib\wit.js:121:36
at process._tickCallback (internal/process/next_tick.js:103:7)
JSON Object returned by API
So I think the error is caused by the execution order, since the code tries to continue within my getMoveReccomendation Function while still executing my omdbapicall.
I think I just have to wait for the function to finish, since it logs the JSON Object after the error, but I don't know how.