T
TanStack3y ago
national-gold

Undefined data returned from useQuery hook

Hello friends! I am trying to get some users from my back end but I get undefined instead. Here is the code :
//fetch users
const { isLoading, error, usersData } = useQuery({
queryKey: ["usersData"],
queryFn: () => fetch("/api/user").then(res => res.json())
});

if (isLoading) return "Loading...";

if (error) return "An error has occurred: " + error.message;

console.log("usersData: ", usersData);
//fetch users
const { isLoading, error, usersData } = useQuery({
queryKey: ["usersData"],
queryFn: () => fetch("/api/user").then(res => res.json())
});

if (isLoading) return "Loading...";

if (error) return "An error has occurred: " + error.message;

console.log("usersData: ", usersData);
Any ideas?
1 Reply
national-gold
national-goldOP3y ago
ok I fixed that by using data: usersData in the first line!

Did you find this page helpful?