Whenever I add a payload api call such as payload.find() to a component with the "use client" directive it gives me a Module not found: Can't resolve 'child_process' error.
Even when I create a separate getObjects.tsx function that I import in the client-side component i get the same error
How do I resolve this ?
I’m not sure if this will be helpful, but I once used this hook to make requests from a component:
https://payloadcms.com/docs/admin/react-hooks#usepayloadapiI don’t know if it’s entirely correct, but it does work 😁
I still seem to be getting the same issue, I can't use the payload utils in client-side components
Did you use this method with the latest version of payloadcms or was this in an earlier version ? 😃
Payload Local API (aka payload.find() etc.) is
server-only, for client you should use REST API
"The Payload Local API gives you the ability to execute the same operations that are available through REST and GraphQL within Node,
directly on your server."
Yes,
But even with the api method (Of using the api endpoint to retrieve the database objects on client components) Lots of dependency errors occur which originate from the payload module files
These errors ONLY occur when I use a payload function (even REST api call) within a client component
show me the code for fetching data, or the code triggering errors
you can't use
payloadobject in client at all
the errors you provided looks like using server utilities on client
The client component file code:
"use client"
import { getLessons } from "@/actions/getLessons";
import { getPayload } from 'payload'
import config from '@payload-config'
import ModuleTimeline from "@/components/module-timeline"
import { usePayloadAPI } from '@payloadcms/ui'
export default async function Page({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params;
const [{ data, isError, isLoading }, { setParams }] = usePayloadAPI(
'/api/lesson/all',
{
initialParams: { depth: 1 },
},
)
console.log(data)
return (
<div className="flex flex-col items-starter justify-start mt-16 min-h-screen py-8 px-32 bg-gray-100">
<h2 className="text-black text-4xl font-bold text-center">
</h2>
<ModuleTimeline />
</div>
);
}The Lesson Collection config:
export const Lesson: CollectionConfig = {
slug: 'lesson',
endpoints: [
{
path: '/all',
method: 'get',
handler: async (req, res, next) => {
try {
const lessons = await req.payload.find({
collection: 'lesson',
depth: 2, // Include related data 2 levels deep
});
res.status(200).json(lessons);
} catch (error) {
next(error);
}
}
}
],
//... The rest of the collection codeYes but they are all from the payloadcms utils, so I'm not sure why there are conflicts
remove getPayload and config imports
and it should be fine
Perfect 🤩,
That seems to have solved the issue
+ another thing to keep in mind is that the component shouldn't be async
I used this instead:
export default function Page() {
const params = useParams<{id:string}>;
const id = params.id;Thanks
@299531596036571136and
@375533665167605760🙏🏻 🙏🏻
yeah client components can't be async, that's just next.js rules - it's not Payload specific
Star
Discord
online
Get dedicated engineering support directly from the Payload team.