The last few days I’ve been dealing with an error ...
# orm-help
s
The last few days I’ve been dealing with an error trying to add a new model to my schema, and I went through a very interesting troubleshooting process to find the solution. TL;DR - Either multiple Prisma clients or an extra node server running at
localhost:3000
was causing PrismaClient and my project to be out of sync. I’ll do my best to describe what happened in a thread 👇
I have a project using SvelteKit, with Postgres as the db and Prisma as the ORM. To start the server I run the command
npm run dev
, and my app is served at
localhost:3000
For weeks I have been updating the schema and migrating with no issues. A few days ago I added a new
Model
(User, since I’m adding auth). I set up my endpoint, which calls
prisma.user.create({ … })
Immediately, I was getting the error:
Copy code
Cannot read properties of undefined (reading 'create')
TypeError: Cannot read properties of undefined (reading 'create')
    at Module.api (/Users/.../Projects/.../src/routes/users/_api.ts:24:35)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async render_endpoint (file:///Users/.../Projects/.../.svelte-kit/runtime/server/index.js:157:19)
Since I’m using TypeScript, I can usually hover the different objects, variables, etc. in my IDE to investigate any typing or composition errors. In this instance I was hovering both
prisma
and
user
in
prisma.user.create()
, and seeing the associated data and methods (see https://imgur.com/a/zkXkc8d and https://imgur.com/a/dDQ0XEF). I was also able to run
npx prisma studio
, and my new
User
model was there.
This made the error
Cannot read properties of undefined (reading 'create')
extra confusing, since all evidence pointed to Prisma having generated my update to the schema successfully, and furthermore, after migrating, Postgres indeed had my new
User
table.
To troubleshoot, I triple-checked my schema and project for typos or misconfigurations and re-ran Prisma
generate
and
migrate
commands, along with trying
prisma migrate reset
. I also rolled back to older migrations, completely removed the /prisma directory and did a re-init. I created and connected a new Postgres db, which only ended up in the same dead ends (
User
model seemed to exist everywhere in my project and db, but I was getting the same error about the
.create()
function). In desperation, I tried older versions of the Prisma and PrismaClient packages, and also completely uninstalling and re-installing Postgres on my machine.
This is where I should mention my first realization - that I may have been conjuring up multiple instances of PrismaClient. I was not following this solution: https://www.prisma.io/docs/support/help-articles/nextjs-prisma-client-dev-practices#solution, which also does happen to work nicely for SvelteKit. Once I realized I may have had the
too many clients
issue brewing, I updated to this approach, but my error was still persisting. At this point in time, I don’t know if this was the cause of the breakdown in generating the PrismaClient, or the other issue I’m about to describe.
👍 1
I started to investigate the one other clue that something was not right -
localhost:3000
was continuing to run even after I had hit Ctrl + C to stop
npm run dev
. This was odd because about a week ago, when I would stop the server with Ctrl + C, the dev tools console would immediately throw errors and page would be dead. Now all of a sudden it wasn’t.
I have limited knowledge of MacOS processes and how to investigate them, but I eventually figured out that I did indeed have something persisting the server at localhost:3000. I killed this process with a terminal command, restarted my project with
npm run dev
, ran
prisma generate
and
prisma migrate dev
(which told me
Already in sync, no schema change or pending migration was found
) and yup, my endpoint with
prisma.user.create()
is now working as expected!
Coming out of this, I have a few questions: • Would
too many clients
be the cause of this issue? • Putting aside my wanting to know how
npm run dev
was not being stopped, would the server not restarting have caused a previously configured PrismaClient to be accessing an old generated version of the schema, even though Prisma Studio at
localhost:5555
was showing me my updated schema? • The
Cannot read properties of undefined (reading 'create')
error did not tell me much about the issue that I assume I was actually dealing with (as far as I know). If one of the two above bullet points was the cause of this issue, is there a better error that could be thrown somewhere else along the chain that could clue a developer in on what was happening?
If you’ve made it this far, thanks for reading 🙏 I need a sandwich 😅
j
• Would
too many clients
be the cause of this issue?
No, that just indicates you are starting to many Prisma Client and its Query Engines internally, which is bad for memory and CPU usage and your database if you keep it up without wanting to do that - hence the error message. When you fixed that code, the restart of your app would have killed all the running Query Engines as well.
• Putting aside my wanting to know how
npm run dev
was not being stopped, would the server not restarting have caused a previously configured PrismaClient to be accessing an old generated version of the schema, even though Prisma Studio at
localhost:5555
was showing me my updated schema?
That sounds indeed reasonable. That old application instance probably had older code loaded, and just kept service that. Because of hot reloading, that might have only been true for part of the code even (and it sounds to me like you had an old Prisma Client (without
User
), but new code (which tries to access
user
.)
• The
Cannot read properties of undefined (reading 'create')
error did not tell me much about the issue that I assume I was actually dealing with (as far as I know). If one of the two above bullet points was the cause of this issue, is there a better error that could be thrown somewhere else along the chain that could clue a developer in on what was happening?
Do you think this was
prisma.user
being
undefined
and then you calling
create
on that? I think this would be a bit like you trying to access a model that does not exist. Would that return something different? (It would probably fail during TS type checks already in the default case) Maybe try that in your setup to see how that would go.
What confuses me a bit is that you say you started a new
npm run dev
. I would have expected that to use a different prot than 3000 or fail with "port already in use" in most frameworks. Is that not the case for SvelteKit?
s
@janpio That’s what I’m trying to figure out with the SvelteKit folks 🤦. Really not sure if the issue lies with SvelteKit, or something about my node scripts. Although everything I’m running out of
package.json
is pretty boilerplate.
Or it was some freak server issue
re: the third bullet point above, the IDE type-checking was saying
prisma.user
was recognized and even provided the example
findMany()
function (see screenshot above). Which is what made this extra confusing
j
True, the IDE of course had the current code and Prisma Client - but the running server instance somehow did not.