Get database informations inside 'use client' component
Hello, i would like to display categories from my database in my 'use client" component, how can i do ?
i don't understand why i have this error when calling database:
Module not found: Can't resolve 'net'
11 Replies
Be very careful connecting to your database from the clientside! If you're directly connecting to the database, it means that the complete connection info (including password) is easily accessible for anyone using it.
You probably want to have something serverside (during rendering or through an API call) connect to the database to keep that information safe from the public.
If you do want to proceed, can you provide a little more context as to your setup? Next.js, how you're setting up Drizzle, etc. An example repository would be helpful.
Hi, I can add you to my repo if you want
I'm using next
If possible I would like to prevent using a fetch, next is pretty useful mixing client and server side so if I can get database informations from server and then pass to client I would love to learn how to do that
In next, the new pattern is to have Server components do the back end call and pass the data to client components via props
are you using pages or app router?
The fact that he mentions "use client" smells like app router
oh duh
I guess Its app router I donc have /src
Wanna check my repo ?
Might as well, my GitHub is
@ngregrichardson
the code is inside
streeteats/admin/app/dashboard/foodtrucks/create/page.tsx
this is a client side component and i'm calling the "CreateStoreForm ' component
admin/app/dashboard/components/createStoreForm.tsx
and this is where i call a hook asking data to database ( line 102 const categories = getCategories;)
Since this has turned into a Next.js question rather than a Drizzle question and since the repo isn't public, I'm gonna move this to DMs.