0

I am trying to import a script in react from a file but it is returning in a way that I do not want it to return. It is messing up the project. Here is the code for both files:

Certification.js:

const fetchCertification = async (type, id) => {
    if (type === "movie") {
      const { data } = await axios.get(
        `https://api.themoviedb.org/3/movie/${id}/release_dates?api_key=${
          import.meta.env.VITE_API_KEY
        }`
      );
      const filteredData = {
        id: data.id,
        results: data.results.map((x) => ({
          ...x,
          release_dates: x.release_dates.filter(
            (date) => date.certification !== ""
          ),
        })),
      };
      if (
        filteredData?.results?.find(({ iso_3166_1 }) => iso_3166_1 == "GB")
          ?.release_dates[0]?.certification ||
        filteredData?.results?.find(({ iso_3166_1 }) => iso_3166_1 == "US")
          ?.release_dates[0]?.certification !== undefined
      ) {
        return (
          filteredData?.results?.find(({ iso_3166_1 }) => iso_3166_1 == "GB")
            ?.release_dates[0]?.certification ||
            filteredData?.results?.find(({ iso_3166_1 }) => iso_3166_1 == "US")
              ?.release_dates[0]?.certification
        );
      } else {
        return false;
      }
    } else {
      const { data } = await axios.get(
        `https://api.themoviedb.org/3/tv/${id}/content_ratings?api_key=${
          import.meta.env.VITE_API_KEY
        }`
      );
      const filteredData = data?.results?.filter((x) => x.rating !== 0);
      if (
        filteredData?.find(({ iso_3166_1 }) => iso_3166_1 == "GB")?.rating ||
        filteredData?.find(({ iso_3166_1 }) => iso_3166_1 == "US")?.rating !==
          undefined
      ) {
        return (
          filteredData?.find(({ iso_3166_1 }) => iso_3166_1 == "GB")?.rating ||
            filteredData?.find(({ iso_3166_1 }) => iso_3166_1 == "US")?.rating
        );
      } else {
        return false;
      }
    }
  };

Home.jsx:

import {fetchCertification} from Certification.js;

const certificationData = fetchCertification(movie, 13453);
console.log(certificationData);

The console is not returning the movies well for some reason. I should get a string with the certification. Please see if there is anything wrong. Thank you.

  • 1
    What do you mean the movies are not returning "well"? Note since `fetchCertification` is an async function it will return a [Promise object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), ie `certificationData` will be the Promise object.. You then need to use the `.then()` methods with an appropriate callback to get the fetched data, eg `certificationData.then((data)=>console.log(data))`. – Patrick Evans Aug 30 '23 at 20:52
  • @PatrickEvans thanks a lot that seemed to be the problem – Visal Yogaraj Aug 30 '23 at 21:32

0 Answers0