react-query does not return queries when used within a seperate function in Next.js #2714
-
I am not sure if this a Next.js issue. I have the following query: const {
isLoading,
isError,
error,
data: course,
} = useQuery(["courses", id], fetchCourseDetails(id), {
staleTime: 60 * 60 * 1000,
});
const fetchCourseDetails = async id => {
try {
const res = await axios.get(
`{BASE_URL}/api/educator/course/${id}`
);
return res;
} catch (err) {
console.log(err);
}
}; For some reason, when I do it like this, it never fetches the data properly. There is always some sort of error popping up. Either its a 400 (the api works alright) or, something like this;
or, the most recent one, which just says const {
isLoading,
isError,
error,
data: course,
} = useQuery(
["courses", id],
async () =>
axios
.get(`http://localhost:3000/api/educator/course/${id}`)
.then(res => res.data),
{
staleTime: 60 * 60 * 1000,
}
); Notice the change here is that the fetch function is directly used in the |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Ok nevermind I solved the main issue. Really silly, but all I had to do was use another function call as below; const {
isLoading,
isError,
error,
data: course,
} = useQuery(["courses", id], () => fetchCourseDetails(id), {
staleTime: 60 * 60 * 1000,
}); While the page loads with all the components, its still not fetching the data. This is what it shows in the devtools. |
Beta Was this translation helpful? Give feedback.
-
Yes
…On Fri, 24 Sep 2021, 8:52 am Magnuscake, ***@***.***> wrote:
Ok nevermind I solved the main issue. Really silly, but all I had to do
was use another function call as below;
const {
isLoading,
isError,
error,
data: course,
} = useQuery(["courses", id], () => fetchCourseDetails(id), {
staleTime: 60 * 60 * 1000,
});
While the page loads with all the components, its still not fetching the
data. This is what it shows in the devtools.
[image: react-query-no-fetch]
<https://user-images.githubusercontent.com/24540399/134612059-b396499c-0155-4023-9f5b-585c43ecd8fc.png>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2714 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AL4TYEPXP5X2QKZJEGR6ZNTUDPTQFANCNFSM5EU6SJDA>
.
|
Beta Was this translation helpful? Give feedback.
-
Since you catch the error in the queryFn to log it, you transform errors to resolved promises. Either rethrow thw error so RQ can pick it up or better: use the onError callback for logging and remove the try/catch |
Beta Was this translation helpful? Give feedback.
-
Putting this here for an lost souls who stumble upon this in the future. For the first problem, where I was trying to call an external function inside the const {
isLoading,
isError,
error,
data: course,
} = useQuery(["courses", id], fetchCourseDetails(id), {
staleTime: 60 * 60 * 1000,
}); we do pass that function through an anonymous function like this const {
isLoading,
isError,
error,
data: course,
} = useQuery(["courses", id], () => fetchCourseDetails(id), {
staleTime: 60 * 60 * 1000,
}); For my second problem, where the url param was being queried twice, this was a Next.js problem. There is a long discussion about this here that you can checkout. Essentially, since |
Beta Was this translation helpful? Give feedback.
Putting this here for an lost souls who stumble upon this in the future. For the first problem, where I was trying to call an external function inside the
useQuery
instead ofwe do pass that function through an anonymous function like this
For my second problem, where the url param was being queried twice, this was a Next.js problem. There is a long discussion about t…