prisma singleton is undefined
Answered
American Chinchilla posted this in #help-forum
American ChinchillaOP
Hi all, I'm struggling with the prisma singelton with server actions.
Using the singleton code from here:
https://www.prisma.io/docs/orm/more/help-and-troubleshooting/help-articles/nextjs-prisma-client-dev-practices
The singleton is working fine for pages where I'm displaying information -- able to query and show that info. But when I import it here in the server action, it is undefined.
If I manually create the prisma client
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
then I can get the server action to process.
Any thoughts? Thanks!
Using the singleton code from here:
https://www.prisma.io/docs/orm/more/help-and-troubleshooting/help-articles/nextjs-prisma-client-dev-practices
The singleton is working fine for pages where I'm displaying information -- able to query and show that info. But when I import it here in the server action, it is undefined.
If I manually create the prisma client
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
then I can get the server action to process.
Any thoughts? Thanks!
Answered by American Chinchilla
Ok, I figured it out after looking at some code on Github! rookie mistake. When I was importing, I was typing
Thanks Abdellah for being a thought partner on this one! 🙌
import { prisma } from .. but the variable prisma was the default export, so the right way to import in another file is just import prisma from ... Thanks Abdellah for being a thought partner on this one! 🙌
6 Replies
English Setter
@American Chinchilla taking a quick look at the linked page, it seems that the
prismaClientSingleton function does nothing to "remember" the creation of the singleton (ie. set the globalThis.prisma) could that be the issue?American ChinchillaOP
I'm not an expert at this at all... but isn't this line saving to the
I'm also baffled because the prisma singleton seems to be working fine in other parts of my codebase.
globalThis object: if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prismaI'm also baffled because the prisma singleton seems to be working fine in other parts of my codebase.
English Setter
my brain shut down after the export 😂 , sorry
how about moving the export all the way down to the bottom of the file?
how about moving the export all the way down to the bottom of the file?
Also put a console.log in the prismaClientSingleton to see how many times/when it gets executed
American ChinchillaOP
Ok, I figured it out after looking at some code on Github! rookie mistake. When I was importing, I was typing
Thanks Abdellah for being a thought partner on this one! 🙌
import { prisma } from .. but the variable prisma was the default export, so the right way to import in another file is just import prisma from ... Thanks Abdellah for being a thought partner on this one! 🙌
Answer
@American Chinchilla Ok, I figured it out after looking at some code on Github! rookie mistake. When I was importing, I was typing `import { prisma } from ..` but the variable `prisma` was the default export, so the right way to import in another file is just `import prisma from ...`
Thanks Abdellah for being a thought partner on this one! 🙌
English Setter
It happens 😂 , glad you found a solution