Prisma 抛出错误“TypeError: cannot read property findmany of undefined”

Posted

技术标签:

【中文标题】Prisma 抛出错误“TypeError: cannot read property findmany of undefined”【英文标题】:Prisma throws an error "TypeError: cannot read property findmany of undefined" 【发布时间】:2020-11-07 04:57:04 【问题描述】:

我想制作一个聊天应用程序来练习使用 graphql 和 node,对于我使用 prisma 的数据库。我正在做本教程中的所有操作。

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

我只是更改了变量名。

所以我有这个代码

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`))

作为我的 index.js

这是我的 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()
  )

和 schema.graphql

type Query 
  history: [Message!]!


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


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

这就是我在操场上得到的

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

请帮忙

【问题讨论】:

【参考方案1】:

我设法解决了这个问题。实际上,我所需要的只是使用与 schema.prisma 中相同但小写的名称

【讨论】:

以上是关于Prisma 抛出错误“TypeError: cannot read property findmany of undefined”的主要内容,如果未能解决你的问题,请参考以下文章

prisma deploy --env-file variables.env 抛出错误

Prismy deploy 命令抛出 Invalid Version 错误

设置数组时缺少必需的值

具有嵌套 connectOrCreate 的 Prisma 多个创建查询抛出唯一约束失败

Prisma:在突变解析器中使用计数

GraphQL 突变抛出错误