Search code examples
node.jstypescriptmongodbtypeerrorprisma

Prisma MongoDB start from scratch error,Object literal may only specify known properties, and 'comments' does not exist in type


I followed the prisma mongodb guide and was able to add the data and confirm the connection., but at the last part to updating a post, I get the following error and cannot finish the guide. What could be the cause? I checked prisma's github issues but could not find a similar one.

return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError: ⨯ Unable to compile TypeScript:
index.ts:20:11 - error TS2322: Type '{ comments: { createMany: { data: { comment: string; }[]; }; }; }' is not assignable to type '(Without<PostUpdateInput, PostUncheckedUpdateInput> & PostUncheckedUpdateInput) | (Without<...> & PostUpdateInput)'.
  Object literal may only specify known properties, and 'comments' does not exist in type '(Without<PostUpdateInput, PostUncheckedUpdateInput> & PostUncheckedUpdateInput) | (Without<...> & PostUpdateInput)'.

 20           comments: {
              ~~~~~~~~~~~
 21             createMany: {
    ~~~~~~~~~~~~~~~~~~~~~~~~~
...
 26             },
    ~~~~~~~~~~~~~~
 27           },
    ~~~~~~~~~~~

// The similar error message follows below for 
comments: true,

prisma index.ts

import { PrismaClient } from "@prisma/client"

const prisma = new PrismaClient()

main()
    .then(async () => {
        await prisma.$disconnect()
    })
    .catch(async (e) => {
        console.error(e)
        await prisma.$disconnect()
        process.exit(1)
    })
async function main() {
    await prisma.post.update({
        where: {
            slug: "my-first-post",
        },
        data: {
            comments: {
                createMany: {
                    data: [
                        { comment: "Great post!" },
                        { comment: "Can't wait to read more!" },
                    ],
                },
            },
        },
    })
    const posts = await prisma.post.findMany({
        include: {
            comments: true,
        },
    })

    console.dir(posts, { depth: Infinity })
}

prisma.shema is here a just copyed from guide

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

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

model Post {
  id       String    @id @default(auto()) @map("_id") @db.ObjectId
  slug     String    @unique
  title    String
  body     String
  author   User      @relation(fields: [authorId], references: [id])
  authorId String    @db.ObjectId
}

model User {
  id      String   @id @default(auto()) @map("_id") @db.ObjectId
  email   String   @unique
  name    String?
  address Address?
  posts   Post[]
}

// Address is an embedded document
type Address {
  street String
  city   String
  state  String
  zip    String
}

environment

Environment variables loaded from .env
prisma                  : 4.8.1
@prisma/client          : 4.8.1
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine d6e67a83f971b175a593ccc12e15c4a757f93ffe (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli d6e67a83f971b175a593ccc12e15c4a757f93ffe (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core d6e67a83f971b175a593ccc12e15c4a757f93ffe (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt d6e67a83f971b175a593ccc12e15c4a757f93ffe (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Format Wasm             : @prisma/prisma-fmt-wasm 4.8.0-61.d6e67a83f971b175a593ccc12e15c4a757f93ffe
Default Engines Hash    : d6e67a83f971b175a593ccc12e15c4a757f93ffe
Studio                  : 0.479.0

Solution

  • It was a mistake in the tutorial documentation.worked well after rewriting the schema. I posted Isuue and it's has been accepted.

    https://github.com/prisma/docs/issues/4445