Search code examples
javascripttypescriptnext.jsprismasupabase

Prisma error TypeError: Cannot read properties of undefined (reading 'findMany')


I've been building a dashboard app with Prisma, Next.js and supabase.

Somehow EventChart model in schema.prisma didn't work, so I decided to add a new model EventAreaChart and migrated but when I request a get method the error says that EventAreaChart is undefined.

Here's the error:

TypeError: Cannot read properties of undefined (reading 'findMany')
    at GET (webpack-internal:///(rsc)/./app/api/eventChart/route.ts:18:55)

route.ts file:

import { NextResponse } from "next/server"
import { main } from "../route"
import { PrismaClient } from "@prisma/client"

const prisma = new PrismaClient

export const GET = async (req: Request, res: NextResponse ) => {
    try {
      await main()
      const eventData = await prisma.eventAreaChart.findMany({
        select: {
          date: true,
          userId: true,
          actual: true,
          expected: true,
        }
      })
      return NextResponse.json({ message: "Success", eventData }, {status: 200})
    } catch (err) {
      console.error('Error in GET method:', err);
      return NextResponse.json({ message: "Error", err }, {status: 500})
    } finally {
      await prisma.$disconnect()
    }
  }

schema.prisma:

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

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

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_DATABASE_URL")
}

model User {
  id          Int @id @default(autoincrement())
  username    String @unique
  password    String
  email       String
  fees        Fee?
  eventChart  EventChart[]
  eventAreaChart  EventAreaChart[]
  memberChart MemberChart[]
  task        Task[]
  event       Event[]
}

model Fee {
  id          Int @id @default(autoincrement())
  userId      Int @unique
  inputFee    Int
  inputPeople Int
  feeResult   Int
  user        User @relation(fields: [userId], references: [id])
}

model EventChart {
  id       Int @id @default(autoincrement())
  userId   Int
  date     String
  expected Int
  actual   Int
  user     User @relation(fields: [userId], references: [id])
}

model EventAreaChart {
  id       Int @id @default(autoincrement())
  userId   Int
  date     String
  expected Int
  actual   Int
  user     User @relation(fields: [userId], references: [id])
}

model MemberChart {
  id        Int @id @default(autoincrement())
  userId    Int
  year      Int
  member    Int
  user      User @relation(fields: [userId], references: [id])
}

model Task {
  id          Int @id @default(autoincrement())
  userId      Int
  date        String
  description String
  check       Boolean
  user        User @relation(fields: [userId], references: [id])
}

model Event {
  id          Int @id @default(autoincrement())
  userId      Int
  date        String
  title       String
  description String
  people      String
  fee         String
  place       String
  user        User @relation(fields: [userId], references: [id])
}

where I'm trying to fetch the data:

"use client"
import { useState, useEffect } from 'react';
import EventEditBtn from './EventEditBtn';

async function fetchEventData() {

    try {
        const res = await fetch(`http://localhost:3001/api/eventChart`, {
        cache: "no-store"
        })

        const data = await res.json()
        console.log(data)

        return data.eventData
    } catch (err) {
        console.error('Error fetching data:', err);
    }
}

const EventAreaChart = () => {

    interface eventDataType {
        id: number,
        userId: number,
        date: string
        expected: number
        actual: number
    }

    useEffect(() => {

        const fetchData = async () => {
            try {
                const data = await fetchEventData()
                setEventData(data)
                console.log(data)
            } catch (error) {
                console.error(error)
            }
        }

        fetchData()

    }, [])

  return (
    <div className="">
        
    </div>
  )
}

export default EventAreaChart

What I tried: -restarted supabase -migration is done after I added EventAreaChart model

please help me out.


Solution

  • The same thing happened to me and the only thing I did was stop the project. Restart TS Server through VsCode and start the project again. Everything worked normally.