kleydon / prisma-session-store

Express session store for Prisma
MIT License
116 stars 18 forks source link

TypeError: Cannot read properties of undefined (reading 'findUnique') #103

Closed Dannyftm closed 1 year ago

Dannyftm commented 1 year ago

Hello,

When I first installed the package and ran my project I got the following error when it tried to start.

/var/nodewww/PROJECT/node_modules/@quixo3/prisma-session-store/dist/lib/prisma-session>
                                .findUnique({
                                 ^
TypeError: Cannot read properties of undefined (reading 'findUnique')
    at PrismaSessionStore.<anonymous> (/var/nodewww/PROJECT/node_modules/@quixo3/prism>
    at step (/var/nodewww/PROJECT/node_modules/@quixo3/prisma-session-store/dist/lib/p>
    at Object.next (/var/nodewww/PROJECT/node_modules/@quixo3/prisma-session-store/dis>
    at fulfilled (/var/nodewww/PROJECT/node_modules/@quixo3/prisma-session-store/dist/>
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
Node.js v17.9.0

Looking into the code it looks like this is for the return [4 /*yield*/, this.prisma[this.sessionModelName].findUnique() call.

I already had the latest version of prisma and express-session installed so I didn't install them when I installed prisma-session-store.

kleydon commented 1 year ago

Hi @Dannyftm, thanks for this. Does your prisma.schema file have a session model defined?

model Session {
  id        String   @id
  sid       String   @unique
  data      String
  expiresAt   DateTime
}

If you can post the "set-up" code you are using, hopefully someone can take a look, or will have an idea what's going on.

Dannyftm commented 1 year ago

I do have that, I added that before adding the package into my project.

model sessions {
  id        String   @id @db.VarChar(255)
  sid       String   @unique @db.VarChar(255)
  data      String   @db.Text
  expiresAt DateTime @map("expires_at")
}

This is how I have mine set up. Also I should add I'm using MySQL for my database.

Dannyftm commented 1 year ago

Ah, got it. The name of the table has to be Session Me changing it to sessions is what broke it because it couldn't find the table sessions that I had set it to.

kleydon commented 1 year ago

Glad to hear things are working!

madooei commented 1 year ago

I have the same issue, but my data model is correctly set up. image

This is my data model image

And I verified the table exists in the production database. Any thoughts on what might be causing this?

kleydon commented 1 year ago

Hi @madooei - thanks for the report. No, I don't currently know what could be causing this.

One thought: Could it be that the findUnique() function is being called at a time when the prisma client is not created, or not fully created? (The fact that the call is being interpreted as <undefined>.findUnique() might suggest this...)

madooei commented 1 year ago

Thanks for the prompt reply @kleydon!

One thought: Could it be that the [..] the prisma client is not created, or not fully created?

I thought that might be the case, so I added npx prisma generate to the deployment pipeline. It runs it before running the Express app. That should create the client (I don't know about "fully created").

It's a strange situation!

kleydon commented 1 year ago

I don't know about "fully created"

(Here I was just thinking about "race conditions" - say one process is building/rebuilding the prisma client, while another, process is making a request that depends on the prisma client existing... Just a thought.)