【问题标题】:Prisma throws an error "TypeError: cannot read property findmany of undefined"Prisma 抛出错误“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"
      ]
    }
  ]
}

请帮忙

【问题讨论】:

    标签: javascript node.js graphql prisma prisma-graphql


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 1970-01-01
      • 2016-12-19
      • 2013-04-22
      • 2021-06-25
      • 1970-01-01
      • 2017-04-24
      • 2023-03-15
      • 2020-10-25
      相关资源
      最近更新 更多