stuck at film finder

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()
      //console.log(jsonResponse.genres)
      return jsonResponse.genres
    }
  } catch (error) {
    console.log(error)
  }

}

const getMovies = async () => {
  const selectedGenre = getSelectedGenre()
  1. console.log(selectedGenre)
  const discoverMovieEndpoint = '/discover/movie'
  const requestParams = `?api_key =${tmdbKey}&with_genres=${selectedGenre}`
 2.  const urlToFetch = `${tmdbBaseUrl}${discoverMovieEndpoint}${requestParams}`
  console.log(urlToFetch)
  try {
    response = await fetch(urlToFetch)
    if (response.ok) {
      const jsonResponse = await response.json()
      console.log(jsonResponse.results)
    }
  } catch (error) {
    console.log(error)
  }

}
getMovies()

401 error from line2
and empty string printed from line1
can anyone help

line 2 should be above the code you provided.

My line 2 is:

const tmdbBaseUrl = 'https://api.themoviedb.org/3';

is this what you have?

1 Like

Do you mind sharing the entire file? It looks like this is only part of your code. Definitely do not share your API key. If you can provide instructions on how to obtain one that would be best.

From what you provided, a 401 response means that you are not providing the right credentials. Basically your HTTP request is failing authentication. It looks like the API is expecting the key to be URL encoded. So maybe you’re not passing the key correctly.

getMovies is also an asynchronous function. So I wonder if you need to write await getMovies(). I’m not sure though.

Thanks

Also, in getMovies, you seem to have an extra space,

// You wrote:
const requestParams = `?api_key =${tmdbKey}&with_genres=${selectedGenre}`

// Change it to:
const requestParams = `?api_key=${tmdbKey}&with_genres=${selectedGenre}`