T
TanStack2y ago
absent-sapphire

Query undefined if I reload the page

I'm using rq with nextjs. I'm using the pages router and I'm having the following problem: This is my query:
const classrooms = useQuery({
queryKey: ["classrooms"],
queryFn: () => {
if (!session?.accessToken || !session.apiEndpoint) {
return;
}

return apiClient.get(
"classrooms",
session.accessToken,
session.apiEndpoint
);
},
initialData: rosterData || [],
});
const classrooms = useQuery({
queryKey: ["classrooms"],
queryFn: () => {
if (!session?.accessToken || !session.apiEndpoint) {
return;
}

return apiClient.get(
"classrooms",
session.accessToken,
session.apiEndpoint
);
},
initialData: rosterData || [],
});
rosterData is the data I pass to the page as a prop from getServerSideProps. What happens is: If I navigate to the page using the router, everything works perfect, but if for some reason y reload the page using F5 for example, the query stops working and returns undefined. Does anyone have insight in why is this happening?
1 Reply
rising-crimson
rising-crimson2y ago
!session?.accessToken || !session.apiEndpoint
then this condition is likely falsy also, please don't return undefined from the queryFn. you should see a warning that this isn't allowed

Did you find this page helpful?