Next.js Discord

Discord Forum

Why does Cannot read properties in Prisma

Answered
codingjogo👨💻 posted this in #help-forum
Open in Discord
Avatar
codingjogo👨💻OP
# Tech I use:
* Auth v5
* Prisma with Neon.tech
* Next.JS 14

## Installing
npm install prisma --save-dev

 npm i prisma/client 

 npm install @auth/prisma-adapter 

## Create 👉 /root/lib/db.ts
 import { PrismaClient } from '@prisma/client';

declare const global: Global & { prisma?: PrismaClient };

export let db: PrismaClient;

if (typeof window === 'undefined') {
  if (process.env['NODE_ENV'] === 'production') {
    db = new PrismaClient();
  } else {
    if (!global.prisma) {
      global.prisma = new PrismaClient();
    }
    db = global.prisma;
  }
} 


## On the red arrow on the image where i get the error:thinq:

# BUT already do the:
npx prisma generate


### Update:
## in /root/prisma/schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
  provider = "prisma-client-js"
}

// prisma/schema.prisma
datasource db {
  provider  = "postgresql"
  url          = env("DATABASE_URL")
  // uncomment next line if you use Prisma <5.10
  // directUrl = env("DATABASE_URL_UNPOOLED")
}

model Account {
  id                 String  @id @default(cuid())
  userId             String
  // ... other codes

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model User {
  id            String    @id @default(cuid())
  // ... other codes
  password      String?
  accounts      Account[]
}
Image
Image
Answered by Clown
Also instead of doing whatever you are doing in that db.ts file, use this:
import { PrismaClient } from '@prisma/client'

const prismaClientSingleton = () => {
  return new PrismaClient()
}

declare global {
  var prisma: undefined | ReturnType<typeof prismaClientSingleton>
}

const prisma = globalThis.prisma ?? prismaClientSingleton()

export default prisma

if (process.env.NODE_ENV !== 'production') globalThis.prisma = prisma
View full answer

5 Replies

Avatar
Clown
Did you do prisma migrate? What does your prisma schema looks like?
Avatar
codingjogo👨💻OP
@Clown I just updated the problem, check it out to see the codes inside the file of scheme.prisma 👀
Avatar
Clown
Do a prisma migrate
Not just generate
Avatar
Clown
Also instead of doing whatever you are doing in that db.ts file, use this:
import { PrismaClient } from '@prisma/client'

const prismaClientSingleton = () => {
  return new PrismaClient()
}

declare global {
  var prisma: undefined | ReturnType<typeof prismaClientSingleton>
}

const prisma = globalThis.prisma ?? prismaClientSingleton()

export default prisma

if (process.env.NODE_ENV !== 'production') globalThis.prisma = prisma
Answer