Prisma throws an error "TypeError: cannot read property findmany of undefined"
Asked Answered
A

5

10

I wanted to make a chat app to practice working with graphql and node, for database I used prisma. I was doing everything like in this tutorial.

https://www.howtographql.com/graphql-js/0-introduction/

I just changed variable names.

so I have this code

const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()


const resolvers = {
  Query: {
    history: async (parent, args, context) => {
      return context.prisma.Messages.findMany()
    },
  },
  Mutation: {
    post: (parent, args, context) => {
      const newMessage = context.prisma.Messages.create({
        data: {
          username: args.username,
          message: args.message,
        },
      })
      return newMessage
    },
  },
}

const server = new GraphQLServer({
  typeDefs: './src/schema.graphql',
  resolvers,
  context: {
    prisma,
  }
})
server.start(() => console.log(`Server is running on http://localhost:4000`))

as my index.js

this is my schema.prisma

  provider = "sqlite"
  url      = "file:./dev.db"
}


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


model Message {
  id       Int      @id @default(autoincrement())
  sendedAt DateTime @default(now())
  message  String
  username String
}

script.js

const { PrismaClient } = require("@prisma/client")


const prisma = new PrismaClient()


async function main() {
  const newMessage = await prisma.Messages.create({
    data: {
      message: 'Fullstack tutorial for GraphQL',
      username: 'www.howtographql.com',
    },
  })
  const allMessages = await prisma.Messages.findMany()
  console.log(allMessages)
}


main()
  .catch(e => {
    throw e
  })
  // 5
  .finally(async () => {
    await prisma.disconnect()
  })

and schema.graphql

type Query {
  history: [Message!]!
}

type Mutation {
  post(username: String!, message: String!): Message!
}

type Message {
  id: ID!
  message: String!
  username: String!
}

and that is what i got in my playground

  "data": null,
  "errors": [
    {
      "message": "Cannot read property 'findMany' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "history"
      ]
    }
  ]
}

please help

Argufy answered 17/7, 2020 at 11:30 Comment(0)
F
10

It should be noted it is not actually lowercase but camel case.

Example:

Model name: Message -> Prisma client name: message

Model name: MessagePerUser -> Prisma client name: messagePerUser

Fiducial answered 29/11, 2022 at 23:59 Comment(0)
A
4

I managed to fix that. Actually, all I needed was to use the same name but lowercased as in schema.prisma

Argufy answered 17/7, 2020 at 11:52 Comment(1)
The same name for what – can you elaborate what exactly was the error?Symphonic
I
1

Would like to add on to the answer

Basically when calling await prisma.User.create, prisma allows the User model to be in caps, but when calling other models such as prisma.messages.create the m has to be in lowercase. Essentially,for all prisma calls, the models should be in lowercase. Hopefully this answers your question, prisma does not flag this error out

Imes answered 4/9, 2022 at 8:39 Comment(0)
O
0

use Table name as camelCase in prisma Client to make Queries

Example: Table Name is: StudentData

then use camelCase according to table name prisma.studentData.findMany();

Obviate answered 1/12, 2022 at 16:18 Comment(0)
G
0

const { PrismaClient } = require('@prisma/client') const prisma = new PrismaClient()

so this in an imp part i resolved my issue just by doing this

Gypsie answered 28/11, 2023 at 19:53 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Epeirogeny
Hello, please don't post code only and add an explantation as to why you think that this is the optimal solution. People are supposed to learn from your answer, which might not occur if they just copy paste code without knowing why it should be used.Para

© 2022 - 2024 — McMap. All rights reserved.