Calling server action using useQuery?
'use client';
import { useQuery } from '@tanstack/react-query';
export const runtime = 'edge';
export async function getCustomer(userId: string): Promise<any> {
"use server";
const customerInfo = (
await db
.select()
.from(customerTable)
.where(eq(customerTable.userId, userId))
)[0];
return Response.json(customerInfo);
}
export default function Page({ params }: { params: { userId: string } }) {
const { data } = useQuery({
queryKey: ['publicCustomer', userId],
queryFn: async () => {
const response = await getCustomer(params.userId);
const data = await response.json();
return data;
},
});
return (
<div>{data}</div>
);
}
'use client';
import { useQuery } from '@tanstack/react-query';
export const runtime = 'edge';
export async function getCustomer(userId: string): Promise<any> {
"use server";
const customerInfo = (
await db
.select()
.from(customerTable)
.where(eq(customerTable.userId, userId))
)[0];
return Response.json(customerInfo);
}
export default function Page({ params }: { params: { userId: string } }) {
const { data } = useQuery({
queryKey: ['publicCustomer', userId],
queryFn: async () => {
const response = await getCustomer(params.userId);
const data = await response.json();
return data;
},
});
return (
<div>{data}</div>
);
}
⨯ Internal error: Error: Only plain objects, and a few built-ins, can be passed to Client Components from Server Components. Classes or null prototypes are not supported.
⨯ Internal error: Error: Only plain objects, and a few built-ins, can be passed to Client Components from Server Components. Classes or null prototypes are not supported.
useQuery
?
- If so, how?1 Reply
like-goldOP•16mo ago
Solved.
Mistake #1:
should be
Mistake #2:
should be
return Response.json(customerInfo);
return Response.json(customerInfo);
return customerInfo;
return customerInfo;
const response = await getCustomer(params.userId);
const data = await response.json();
return data;
const response = await getCustomer(params.userId);
const data = await response.json();
return data;
const data = await getCustomer(params.userId);
return data;
const data = await getCustomer(params.userId);
return data;