I am doing building a front end application using react and I am using react-query's useQuery hook to fetch data. Here is the code
import axios from "axios";
import { useQuery } from "@tanstack/react-query";
PROXY_URL = "https://cors-anywhere.herokuapp.com/"
API_URL = "";
export const useProducts = () => {
const { data, isLoading, error } = useQuery({
queryKey: ["products"],
queryFn: async () => {
try {
const response = await axios.get(`${PROXY_URL}{API_URL}`);
return response.data;
} catch (error) {
console.log(`Server error: ${error}`);
throw error;
}
},
retry: 2,
retryOnMount: true,
});
return {
isLoading,
error: error || (data && data.error),
products: data?.data?.products || [],
};
};
If I use proxy url "https://cors-anywhere.herokuapp.com/" it fetches the data temporarily but not reliable for long term use. I do not have access to the backend api for change cors configuration. This error is really stopping me from building rest of the application. Can someone please help me figure out why I am getting this error and how I can fix it? Thank you in advance for your help.