Data from useQuery has wrong type but returns correct values #6421
Unanswered
mateuscqueiros
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hii, from Bard AI The issue lies in the way you're handling the Axios response in the fetchProjects function. When you return the entire Axios response object, it's not providing the expected data structure that React Query expects. To fix this, you need to explicitly extract the data from the Axios response and return it. Here's the modified fetchProjects function that correctly returns the data: async function fetchProjects(
resourceId: number,
{ currentPage, pageSize, search }: fetchProjectsParams
) {
const response = await axios.get(
`/api/resources/${resourceId}/projects`,
{
params: {
page: currentPage - 1,
pageSize,
search,
},
}
);
// Extract data from the Axios response
const { data } = response;
// Return only the data
return data;
} With this modification, the fetchProjects function will return the expected data structure, and React Query will be able to correctly handle the response and provide the data to your component. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have the following function that fetches data:
And the following implementation of useQuery:
Then I use the query like this:
When ignoring the errors and running the code, the data is fetched and displayed correctly on screen.
To solve the errors on VS Code I modified fetchProjects to the following:
With this, the errors disappear, but the data is not being displayed anymore and a new error appears on the console:
I know the data is being fetched by Axios because it's appearing on the Network Tab.
I also tried modifying fetchProjects to:
But the results are the same: no errors on VS Code, data is fetched but react-query is not displaying it.
Apparently I'm not returning anything to queryFn. Trying to return some mockData directly to queryFn solves both problems, but not with Axios Requests.
What can I do?
Beta Was this translation helpful? Give feedback.
All reactions