Search code examples
reactjstypescriptnext.jsprisma

Object literal may only specify known properties in TS Next.js 14 APP


In my typescript next.js 14 APP I got this error. Please help me to fix this error.

Object literal may only specify known properties, and 'productPackages' does not exist in type '(Without<ProductCreateInput, ProductUncheckedCreateInput> & ProductUncheckedCreateInput) | (Without<...> & ProductCreateInput)'.ts(2353) index.d.ts(4685, 5): The expected type comes from property 'data' which is declared here on type '{ select?: ProductSelect | null | undefined; include?: ProductInclude | null | undefined; data: (Without<...> & ProductUncheckedCreateInput) | (Without<...> & ProductCreateInput); }' (property) productPackages: any No quick fixes available

Please check the below screenshot

enter image description here

    import prisma from "@/libs/prismadb";
    import { NextResponse } from "next/server";
    import { getCurrentUser } from "@/actions/getCurrentUser";
    
    export async function POST(request: Request) {
      const currentUser = await getCurrentUser();
    
      if (!currentUser) return NextResponse.error();
    
      if (currentUser.role != "ADMIN") {
        return NextResponse.error();
      }
    
      const body = await request.json();
      const {
        name,
        description,
        price,
        brand,
        type,
        productPackages,
        availablequantity,
        soldquantity,
        category,
        publish,
        images,
      } = body;
    
      const product = await prisma.product.create({
        data: {
          name,
          description,
          brand,
          type,
          productPackages,
          availablequantity: parseFloat(availablequantity),
          soldquantity: parseFloat(soldquantity),
          category,
          publish,
          images,
          price: parseFloat(price),
        },
      });
      return NextResponse.json(product);
    }
    
    export async function PUT(request: Request) {
      const currentUser = await getCurrentUser();
    
      if (!currentUser || currentUser.role != "ADMIN") {
        return NextResponse.error();
      }
    
      const body = await request.json();
      const { id, publish } = body;
    
      const product = await prisma.product.update({
        where: { id: id },
        data: { publish },
      });
      return NextResponse.json(product);
    }
datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

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

model Account {
  id                 String  @id @default(auto()) @map("_id") @db.ObjectId
  userId             String  @db.ObjectId
  type               String
  provider           String
  providerAccountId  String
  refresh_token      String?  @db.String
  access_token       String?  @db.String
  expires_at         Int?
  token_type         String?
  scope              String?
  id_token           String?  @db.String
  session_state      String?

  user User @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@unique([provider, providerAccountId])
}

model User {
  id                 String    @id @default(auto()) @map("_id") @db.ObjectId
  name               String?
  email              String?   @unique
  emailVerified      DateTime?
  image              String?
  hashedPassword     String?
  createdAt          DateTime @default(now())
  updatedAt          DateTime @updatedAt
  role Role @default(USER)


  accounts      Account[]
  orders Order[]
  reviews Review[]
}

model Product{
  id String @id @default(auto()) @map("_id") @db.ObjectId
  name String
  description String
  price Float
  brand String
  type ProductType[]
  productPackages productPackage[]
  availablequantity Float
  soldquantity Float
  category String
  publish Boolean
  images Image[]
  reviews Review[]
}

type productPackage {
    packaging String
    price Float
}

type ProductType {
  type String
  typeCode String
  image String
}

type Image {
  color String
  colorCode String
  image String
}
model Review{
  id String @id @default(auto()) @map("_id") @db.ObjectId
  userId String @db.ObjectId
  productId String @db.ObjectId
  rating Int
  comment String
  createdDate DateTime @default(now())

  product Product @relation(fields: [productId], references: [id])
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model Order{
  id String @id @default(auto()) @map("_id") @db.ObjectId
  userId String @db.ObjectId
  amount Float 
  currency String
  status String
  deliveryStatus String
  createDate DateTime @default(now())
  paymentIntentId String @unique
  products CartProductType[]
  address Address?

    user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

type CartProductType{
  id String
  name String
  description String
  category String
  brand String
  selectedImg Image
  quantity Int
  price Float
}

type Address{
  city String
  country String
  line1 String
  line2 String
  postal_code String
  state String
}

enum Role{
    USER
    ADMIN
}

Solution

  • You can fix this by creating a variable and casting that variable as the input type that Prisma gives you when you run npx prisma generate.

    import { Prisma } from "@prisma/client";
    
    const data: Prisma.ProductCreateInput = {
       ...
    }
    
    // Pass the data to prisma's create method
    const product = await prisma.product.create({ data });