ynssssssss
ynssssssss

Reputation: 311

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

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

Upvotes: 11

Views: 35847

Answers (5)

SIDDHARTH MANJREKAR
SIDDHARTH MANJREKAR

Reputation: 1

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

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

Upvotes: 0

Gaurav Pawar
Gaurav Pawar

Reputation: 59

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();

Upvotes: 0

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

Upvotes: 10

Lee Leonard
Lee Leonard

Reputation: 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

Upvotes: 1

ynssssssss
ynssssssss

Reputation: 311

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

Upvotes: 4

Related Questions