i have the following function:
const getTotalPages = async () => {
const selectedGenre = getSelectedGenre();
const discoverMovieEndpoint = '/discover/movie';
const requestParams =`api_key=${tmdbKey}&with_genres=${selectedGenre}&total_pages=True`;
const urlToFetch = `${tmdbBaseUrl}${discoverMovieEndpoint}${requestParams}`;
try {
const response = await fetch(urlToFetch);
if (response.ok) {
const jsonResponse = await response.json();
const totalPages = jsonResponse.total_pages;
return totalPages;
}
}
catch(error) {
console.log(error);
}
}
and then i want to use it inside another function like that:
const getMovies = async () => {
const selectedGenre = getSelectedGenre();
const discoverMovieEndpoint = '/discover/movie';
const requestParams = `?api_key=${tmdbKey}&with_genres=${selectedGenre}&page=${Math.ceil(Math.random()*getTotalPages())}`;
const urlToFetch = `${tmdbBaseUrl}${discoverMovieEndpoint}${requestParams}`;
try {
const response = await fetch(urlToFetch);
if (response.ok) {
const jsonResponse = await response.json();
const movies = jsonResponse.results;
return movies;
}
}
catch(error) {
console.log(error);
}
};
so I can i display a random result from a random page, not just from the first page but I'm getting undefined. By the way, getSelectedGenre()
works fine. I've searched here and in tmdb discourse page and found that others before me struggled with similar issue but couldn't find an answer.