0

I have been pulling my hair out for the last day in hoping I can find a way of getting around or fixing this. I have tried everything I can think of, and no matter what, the function always returns undefined.

What I am trying to do is getting the information from The Movie Database, but via a function with a variable of the film's ID number. It retrieves the information perfectly, but the issue is, it won't return the JSON array. It always returns undefined and I'm unsure what is causing this.

 function getMovieData (movieid) {
    var data;

    moviedb.movieInfo({id: movieid}, (err, res) => {
        data = res; 
    })  

    return data;
}

The return value would be returned and would be used via Express and Handlebars to display to the user.

I am genuinely confused as to why this isn't working. Any help would be appreciated.

Sam Bunting
  • 845
  • 1
  • 15
  • 36
  • Seems `movieInfo` is async function – Satpal Jan 30 '17 at 12:57
  • I think you should return within the function. `moviedb.movieInfo({id: movieid}, (err, res) => { return res; })` as its async. – Mike Bovenlander Jan 30 '17 at 12:59
  • Yeah, I thought that. But how would I get it to pass through the `movieid` value? I can't seem to find any documentation on how to do it. – Sam Bunting Jan 30 '17 at 13:06
  • Solved. Created another function which passes through the return data from the API call. The other function would set a variable which I could use in my view. – Sam Bunting Jan 30 '17 at 17:14

0 Answers0