T
TanStack3y ago
ambitious-aqua

How can my query not exist in the queryClient?

Hello, I have this code:
const getDiary = async (userId: string | undefined | null) => {
if (!userId) return null;
let {
data: diary,
error,
count,
} = await supabase
.from("current_diaries")
.select("days")
.eq("uid", userId)
.maybeSingle();

if (error) {
throw new Error(`${error.message}: ${error.details}`);
}
if (diary) {
return diary.days;
}
return initializedStarterDiary;
};

function useGetDiary(userId: string | undefined | null) {
return useQuery({
queryKey: ["currentDiary"],
queryFn: () => getDiary(userId),
enabled: !!userId,
refetchOnMount: false,
});
}

const { data: diary, isLoading, isError } = useGetDiary(userId);

return (
<SafeAreaView edges={["top"]} style={styles.container}>
<Button
title="Log the diary"
onPress={() => {
console.log(diary?.length);
console.log(queryClient.getQueryData(["currentDiary"]));
}}
/>
const getDiary = async (userId: string | undefined | null) => {
if (!userId) return null;
let {
data: diary,
error,
count,
} = await supabase
.from("current_diaries")
.select("days")
.eq("uid", userId)
.maybeSingle();

if (error) {
throw new Error(`${error.message}: ${error.details}`);
}
if (diary) {
return diary.days;
}
return initializedStarterDiary;
};

function useGetDiary(userId: string | undefined | null) {
return useQuery({
queryKey: ["currentDiary"],
queryFn: () => getDiary(userId),
enabled: !!userId,
refetchOnMount: false,
});
}

const { data: diary, isLoading, isError } = useGetDiary(userId);

return (
<SafeAreaView edges={["top"]} style={styles.container}>
<Button
title="Log the diary"
onPress={() => {
console.log(diary?.length);
console.log(queryClient.getQueryData(["currentDiary"]));
}}
/>
When I log the state to the console I get:
7,
undefined
7,
undefined
in the docs it says this: "getQueryData is a synchronous function that can be used to get an existing query's cached data. If the query does not exist, undefined will be returned." How can my query not exist when I literally logged the data from it, what is wrong here?
1 Reply
harsh-harlequin
harsh-harlequin3y ago
where is queryClient coming from ?

Did you find this page helpful?