0

I keep getting this error with NextJS and Prisma when trying to pull the categories for my website (I'm using MongoDB): TypeError: Cannot read properties of undefined (reading 'findMany') at GET

I've looked at so many other posts and websites but no solution is fixing the issue. I have no idea what is wrong. The user data and categories are showing up in Prisma studio and MongoDB but I think there's some issue with the GET method?

Any help appreciated!! Here is my schema:

generator client {
  provider = "prisma-client-js"
  output   = "../app/generated/prisma"
}

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model User {
  id            String          @id @default(cuid()) @map("_id") @db.ObjectId
  name          String?
  email         String          @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  Post          Post[]
  Comment       Comment[]
 
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Account {
  id                String  @id @default(cuid()) @map("_id") @db.ObjectId
  userId            String  @db.ObjectId
  type              String
  provider          String
  providerAccountId String
  refresh_token     String? @db.String
  access_token      String? @db.String
  expires_at        Int?
  token_type        String?
  scope             String?
  id_token          String? @db.String
  session_state     String?
 
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
 
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
 
  @@unique([provider, providerAccountId])
}
 
model Session {
  id           String   @id @default(cuid()) @map("_id") @db.ObjectId
  sessionToken String   @unique
  userId       String   @db.ObjectId
  expires      DateTime
  user         User     @relation(fields: [userId], references: [id], onDelete: Cascade)
 
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
 
model VerificationToken {
  id         String   @id @default(cuid()) @map("_id") @db.ObjectId
  identifier String
  token      String
  expires    DateTime
 
  @@unique([identifier, token])
}

model Category {
  id      String @id @default(cuid()) @map("_id")
  slug    String @unique
  title   String
  img     String?
  Posts   Post[]
}

model Post {
  id        String @id @default(cuid()) @map("_id")
  createdAt DateTime @default(now())
  slug      String @unique
  title     String
  desc      String
  img       String?
  views     Int @default(0)
  catSlug   String
  cat       Category @relation(fields: [catSlug], references: [slug])
  userEmail String
  user      User @relation(fields: [userEmail], references: [email])
  comments  Comment[]
}

model Comment {
  id        String @id @default(cuid()) @map("_id")
  createdAt DateTime @default(now())
  desc      String
  userEmail String
  user      User @relation(fields: [userEmail], references: [email])
  postSlug  String
  post      Post @relation(fields: [postSlug], references: [slug])
}

This is my route code:

import prisma from '@/utils/connect';
import {NextResponse} from 'next/server';

export const GET = async () => {
  try{
    const categories = await prisma.category.findMany();

    return new NextResponse(
      JSON.stringify(categories), {status: 200}
    );

  } catch(err) {
    console.log(err);
    return new NextResponse(
      JSON.stringify({message: "Something went wrong!"}, {status: 500})
    );
  }
};

This is my connect file:

import {PrismaClient} from '@prisma/client';

let prisma;

if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient();
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient();
  }
  prisma = global.prisma;
}

export default prisma;

I made sure the model is the lowercase of the schema model name. I tried the following also:

const categories = await prisma.category.findMany({
  select: {
    id: true,
  }
});

I tried uninstalling Prisma and reinstalling again. Deleted the mongoDB database and made a new one. I tried doing npx prisma db push when I changed the schema, restarted the app many times - still nothing.

I just want the categories to show up on the page.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.