Reputation: 1
Im working on a personal jQuery/ajax/json project, and i am trying to collect data from the TMDB API. Where I want people to search for a certain term, for example the movie Fight Club. What should happen is that see a movie poster which matches the movie title entered.
I got this so far:
$.ajax({ type: "GET", dataType: "jsonp", cache: false, url: "https://api.themoviedb.org/3/search/movie?api_key=257654f35e3dff105574f97fb4b97035&query=fight+club", success: toonTMDBresults // at succes, run the function toonTMDBResults });
function toonTMDBresults(tmdbJson) {
console.log('TMDB:', tmdbJson);
if ( 1 == 1 ) { // if 1 equals 1, so always run the following code... :)
for (var idx in tmdbJson.data) { //loop through the results
var results = tmdbJson.data[idx];
console.log(results);
console.log(results.results[0].id); //take the ID of the first result in the array
}
} else {
console.log('problem with the TMDB request:', json.meta.code);
}
What I wanted to happen is that I see the Id of the movie in my console. If I am able to do that, I would be able to select the poster_path from the json data and show the movie poster in my html.
But the problem is I am not getting the ID in my console as I requested, so i am doing something wrong.
Thanks,
Mark
Upvotes: 0
Views: 281
Reputation: 741
tmdbJson.data
doesn't exist. The object returned is :
TMDB: Object {page: 1, results: Array[10], total_pages: 1, total_results: 10}
You probably meant iterating over tmdbJson.results
. This is a working version of your function:
function toonTMDBresults(tmdbJson) {
console.log('TMDB:', tmdbJson);
console.log('first id: ', tmdbJson.results[0].id);
console.log('first poster path: ', tmdbJson.results[0].poster_path);
if ( 1 == 1 ) { // if 1 equals 1, so always run the following code... :)
for (var idx in tmdbJson.results) { //loop through the results
var results = tmdbJson.results[idx];
console.log(results);
console.log(results.id); //take the ID of the first result in the array
}
} else {
console.log('problem with the TMDB request:', json.meta.code);
}
}
Upvotes: 0