skip to Main Content

I’m new to front end dev and especially to reactjs and I’m trying to fetch json data with this code, but I’m having this in console Uncaught (in promise) ReferenceError: response is not defined Could u help please?

import { useEffect } from "react";

const URL_API = "https://gist.githubusercontent.com/saniyusuf/406b843afdfb9c6a86e25753fe2761f4/raw/523c324c7fcc36efab8224f9ebb7556c09b69a14/Film.JSON";

function App() {
  const searchMovies = async (Title) => {
    const response = await fetch(`${URL_API}&s=${Title}`);
    
    const data = await response.json();

    console.log(data);
  };
  useEffect(() => {
    searchMovies('Avatar');
  }, []);
  return <h1>hello</h1>;
}

export default App;

2

Answers


  1. we need more context but u can handle errors this way :

    const searchMovies = async (Title) => {
      try {
        const response = await fetch(`${URL_API}&s=${Title}`);
        const data = await response.json();
        console.log(data);
      } catch (error) {
        console.error(error);
      }
    };
    
    Login or Signup to reply.
  2. TRY THIS
    
    import { useEffect } from "react";
    
    const URL_API = "https://gist.githubusercontent.com/saniyusuf/406b843afdfb9c6a86e25753fe2761f4/raw/523c324c7fcc36efab8224f9ebb7556c09b69a14/Film.JSON";
    
    function App() {
      const searchMovies = async (Title) => {
        try {
          const response = await fetch(URL_API);
          const data = await response.json();
          console.log(data);
        } catch (error) {
          console.log(error);
        }
      };
    
      useEffect(() => {
        searchMovies('Avatar');
      }, []);
    
      return <h1>hello</h1>;
    }
    
    export default App;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search