Cannot hit a Url and fetch the data.

First of all here is the code: import React, { useEffect, useState } from 'react' import {useParams} from "react-router-dom" import { fetchApi } from '../../api/Api' import "./player.css" const Player = () => { const {movie_id} = useParams() console.log(movie_id) const [data,setData]= useState() useEffect(()=>{ const fetchData = async()=>{ const {data} = await fetchApi(/movie/${movie_id}/videos) setData(data) } fetchData() },[movie_id]) // console.log(data) return ( <div>Player</div> ) } export default Player and now main things, I have already use the same function to fetch a data but there was another url, also when I used the rapid api extesion, I was still able to fetch the data I want, But I don't know what I am doing wrong here, that I cannot get the data and it is giving error 404. Again the function is right and I have already fetched the data with different url and the movie_id is also correct. Still can't fetch the data here.
4 Replies
vince
vince2y ago
Did you try fetching data in Postman to make sure it works correctly? If it works in Postman then it's a problem with your code. But it's pretty much impossible for me, at least, to tell you what your problem is with just a single React component Though that useEffect fetchData() function does look a bit sketchy Could you not just do something like
useEffect(() => {
fetchApi(endpoint).then((data) => setData(data)).catch((error) => throw new Error(error));
}
useEffect(() => {
fetchApi(endpoint).then((data) => setData(data)).catch((error) => throw new Error(error));
}
ronyshahab
ronyshahabOP2y ago
the url I entered in the function is returing error 404 , when I changed the URl, I can get the data, I read the documentation of the TMDB api , but still couldn't find the issue. Bro , I think maybe you have misunderstand something, My issue is still not solved and I need that particular url to hit and get the data, If you have time than please help me, and thanks for trying to help me.
Tenkes
Tenkes2y ago
can we see your fetchApi function?
ronyshahab
ronyshahabOP2y ago
give a momemnt import axios from "axios" const api_key =Dummy key' const Base_url = "https://api.themoviedb.org/3" const Token = ''Dummy key" export const fetchApi = async(url)=>{ try { const {data} = await axios.get(${Base_url}/${url}/?api_key=${api_key})
return data
} catch (error) { console.log(error.message) } } here you go
Want results from more Discord servers?
Add your server