I'm trying to get the response from TMDB API (https://developer.themoviedb.org/reference/search-movie) for search movie query. and i observed my code more than 10 times but getting the same error :: parsed is undefined
I'm getting that searchInput from a Form Component.
function BrowseMovies() {
let responseId = null
const queryFunc = async (options) => {
const response = await axios.request(options);
responseId = response?.data?.results[0]?.id
console.log(response.data.results)
return response.data.results;
};
const onSubmit = (searchInput) => {
let params = {
query: searchInput.query,
include_adult: searchInput.includeAdult.toString(),
language: searchInput.language,
primary_release_year: searchInput.year.toString(),
page: '1',
year: searchInput.year.toString(),
};
const options = {
method: 'GET',
url: 'https://api.themoviedb.org/3/search/movie',
params: params,
headers: {
accept: 'application/json',
Authorization: '-- Key --',
},
};
queryFunc(options);
};
const {data , isLoading , isError , error , refetch} = useQuery({
queryKey : ['searchedMovie' , responseId],
queryFn: queryFunc,
enabled:false,
refetchOnWindowFocus:false,
})
-- rendering Data --
}
responseIdresponseIdin the queryKey['searchedMovie' , responseId]which mean it will be passed as parameters to the queryFn, which is wrongYou should do this instead:
Your fetch API:
Your onSubmit is to get the options, so let rename it to
getOptions:This is how you construct the
useQuery:Then you can call refetch so that
react-querystart fetching the data when users click the submit button: