+ 2
How to get genre ids with genre names in TMDb api?
I'm having issues with showing genres names for each movie displayed.I want each genre_ids from movies api give me names of genres but I am getting only ids which are 0,18 etc and how to convert it into action, adventure. Please help me to solve this in javascript.
17 Antworten
+ 2
To get genre names you need another api fetch
https://code.sololearn.com/WgZHrhC3dhbh/?ref=app
+ 3
Arun Jamson
I went to register one api key, it is actually works. Please check
https://code.sololearn.com/WZQYiojdrjxhB6FKJ/?ref=app
+ 2
Try this
const apiKey = 'your api key';
const movieId = 100;
/**
* Fetch movie data from themoviedb.org
* @param {number} id Movie id
* @return {object} Movie object
*/
async function fetchMovie(id) {
const response = await fetch(`https://api.themoviedb.org/3/movie/${id}?api_key=${apiKey}`);
const movie = await response.json();
return movie;
}
fetchMovie(movieId).then(movie => {
movie.genres.forEach(genre => {
console.log("genre id: " + genre.id + ", genre name: " + genre.name); // output genre and id
})
});
https://code.sololearn.com/WhD17FVXyeVE/?ref=app
+ 2
https://code.sololearn.com/WOzGctmT7QB8/?ref=app
+ 1
In my code it is showing foreach is not defined error
+ 1
Arun Jamson you need to set a valid api key for the code to work.
+ 1
Arun Jamson Then you have an invalid key, try to request again.
0
Well if you are at front end maybe get tag by id?
0
But if u are at backend it depends on library and stuff
0
I am not asking about tags but in api that arrays name is genre_id, please read question properly bro.
0
Calvin bro i didn't get it and in your playground it is showing movies undefined can you explain by showing in codeplayground please.
0
I have put then also it is showing genres not defined
0
Ok
0
Can you explain me with these these code
https://code.sololearn.com/W4q7rZI58G3w/?ref=app
0
And how to get random movies genre in above code please bro help me.
0
Bro but the genre ids are in numbers how to get in strings like action or comedy of same movie
0
Thanks bro