How to get genre ids with genre names in TMDb api? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

6th Aug 2021, 4:58 AM
Arun Jamson
Arun Jamson - avatar
17 Answers
+ 2
To get genre names you need another api fetch https://code.sololearn.com/WgZHrhC3dhbh/?ref=app
9th Aug 2021, 7:05 AM
Calviղ
Calviղ - avatar
+ 3
Arun Jamson I went to register one api key, it is actually works. Please check https://code.sololearn.com/WZQYiojdrjxhB6FKJ/?ref=app
7th Aug 2021, 8:51 AM
Calviղ
Calviղ - avatar
+ 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
6th Aug 2021, 2:45 PM
Calviղ
Calviղ - avatar
+ 2
https://code.sololearn.com/WOzGctmT7QB8/?ref=app
8th Aug 2021, 9:24 AM
Calviղ
Calviղ - avatar
+ 1
In my code it is showing foreach is not defined error
6th Aug 2021, 5:02 PM
Arun Jamson
Arun Jamson - avatar
+ 1
Arun Jamson you need to set a valid api key for the code to work.
7th Aug 2021, 4:18 AM
Calviղ
Calviղ - avatar
+ 1
Arun Jamson Then you have an invalid key, try to request again.
7th Aug 2021, 4:25 AM
Calviղ
Calviղ - avatar
0
Well if you are at front end maybe get tag by id?
6th Aug 2021, 5:22 AM
Abs Sh
Abs Sh - avatar
0
But if u are at backend it depends on library and stuff
6th Aug 2021, 5:23 AM
Abs Sh
Abs Sh - avatar
0
I am not asking about tags but in api that arrays name is genre_id, please read question properly bro.
6th Aug 2021, 12:58 PM
Arun Jamson
Arun Jamson - avatar
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.
6th Aug 2021, 4:54 PM
Arun Jamson
Arun Jamson - avatar
0
I have put then also it is showing genres not defined
7th Aug 2021, 4:20 AM
Arun Jamson
Arun Jamson - avatar
0
Ok
7th Aug 2021, 9:11 AM
Arun Jamson
Arun Jamson - avatar
0
Can you explain me with these these code https://code.sololearn.com/W4q7rZI58G3w/?ref=app
7th Aug 2021, 4:58 PM
Arun Jamson
Arun Jamson - avatar
0
And how to get random movies genre in above code please bro help me.
8th Aug 2021, 6:57 AM
Arun Jamson
Arun Jamson - avatar
0
Bro but the genre ids are in numbers how to get in strings like action or comedy of same movie
8th Aug 2021, 10:58 AM
Arun Jamson
Arun Jamson - avatar
0
Thanks bro
9th Aug 2021, 7:08 AM
Arun Jamson
Arun Jamson - avatar