Film finder project

Hello everyone! I cannot carry out the project correctly. When I press the button nothing really happens. Can anyone tell me where my error is?

const tmdbKey = ’ '; // API key
const tmdbBaseUrl = ‘https://api.themoviedb.org/3’; //API’s URL base
const playBtn = document.getElementById(‘playBtn’);

const getGenres = async () => {
const genreRequestEndpoint = ‘/genre/movie/list’;
const requestParams = ?api_key=${tmdbKey};
const urlToFetch = ${tmdbBaseUrl}${genreRequestEndpoint}${requestParams};

try {
    const response = await fetch(urlToFetch);

    if(response.ok) {
        const jsonResponse = await response.json();
        const genres = jsonResponse.genres
        return genres;
    }
} catch (error) {
    console.error(error);
};

};

const getMovies = async () => {
const selectedGenre = getSelectedGenre();
const discoverMovieEndPoint = ‘/discover/movie’;
const requestParams = ?api_key=${tmdbKey}&with_genres=${selectedGenre};

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);
};

};

const getMovieInfo = async (movie) => {
const movieId = movie.id;
const movieEndpoint = /movie/${movieId};
const requestParams = ?api_key=${tmdbKey};
const urlToFetch = ${tmdbBaseUrl}${movieEndpoint}${requestParams};

try {
    const response = await fetch (urlToFetch);
    if(response.ok) {
        const jsonResponse = await response.json();
        const movieInfo = jsonResponse;
        return movieInfo;
    }
} catch (error) {
    console.error(error);
}

};

// Gets a list of movies and ultimately displays the info of a random movie from the list
const showRandomMovie = async () => {
const movieInfo = document.getElementById(‘movieInfo’);
if (movieInfo.childNodes.length > 0) {
clearCurrentMovie();
};

const movies = await getMovies();
const randomMovie = getRandomMovie(movies);
const info = await getMovieInfo(randomMovie);
displayMovie(info);

};

getGenres().then(populateGenreDropdown);
playBtn.onclick = showRandomMovie;

This is the console error:

image

Hi,

Set a breakpoint in your browser. You can do this so that you follow the train of what your variable’s value is and troubleshoot from there.

1 Like

In getMovies, you haven’t declared and initialized urlToFetch before trying to fetch it.

1 Like

Please still learn how to use the browser debugger. It’ll save you so much time and effort.

3 Likes

That’s it! Thank you so much.

1 Like

Thank you, i 'll try it next time.

1 Like