Luciano Balbiano
Luciano Balbiano

Reputation: 51

TypeError: The "payload" argument must be of type object. Received null

My scheme on prisma:

/// This model or at least one of its fields has comments in the database, and requires an additional setup for migrations: Read more: https://pris.ly/d/database-comments
model articles {
  id                  String        @id @default(uuid())
  dealership_id       String
  type                String?       @db.VarChar(255)
  brand               String?       @db.VarChar(255)
  model               String?       @db.VarChar(255)
  year                Int?
  vin                 String?       @db.VarChar(255)
  registration_number String?       @db.VarChar(255)
  purchase_price      Decimal?      @db.Decimal(10, 0)
  selling_price       Decimal?      @db.Decimal(10, 0)
  maintenance_needed  Decimal?      @db.Decimal(10, 0)
  maintenance_done    Decimal?      @db.Decimal(10, 0)
  condition           String?       @db.VarChar(255)
  description         String?       @db.Text
  maintenance_date    DateTime?     @db.Timestamp(0)
  created_at          DateTime?     @default(now()) @db.Timestamp(0)
  updated_at          DateTime?     @updatedAt @db.Timestamp(0)

  dealership          dealerships   @relation(fields: [dealership_id], references: [id]) // Relación con concesionaria
  purchases           purchases[]   
  sales               sales[]       
  article_photos      article_photos[] // Relación con fotos de artículo

  @@index([dealership_id], map: "dealership_id")
}

and my POST method on route.ts:

import prisma from '@/utils/prisma';
import { NextResponse } from 'next/server';

export async function POST(req: Request) {
  try {
    // Verifica si estás usando Next.js 13+
    const body = await req.json(); // Extrae el JSON del request

    console.log('Body recibido:', body); // Log para depuración

    // Validar el contenido del body
    if (!body.type || !body.brand || !body.model) {
      return NextResponse.json(
        { error: 'Todos los campos son obligatorios' },
        { status: 400 }
      );
    }

    // Crear artículo
    const article = await prisma.articles.create({
      data: {
        ...body,
        dealership_id: 'd9078ca9-4d6e-4d0e-a60d-dabbb3c8e5c2', // ID fijo para prueba
      },
    });

    return NextResponse.json(article, { status: 201 });
  } catch (error) {
    console.error('Error:', error);
    return NextResponse.json(
      { error: 'Error al registrar el artículo' },
      { status: 500 }
    );
  }
}

I already tried creating new migrations in db or "npx prisma db push" and it didn't solve my problem, it only happens on this entity, for example, I created a dealership without problems and used its uuid to try to create an article.

Upvotes: 5

Views: 4977

Answers (5)

yeahdixon
yeahdixon

Reputation: 6934

I actually had version mismatchs in the package manager. I made sure the prisma/client and the primsa versions where the same

Upvotes: 0

Joss Bird
Joss Bird

Reputation: 311

I found that I was connecting to the wrong database. My Prisma CLI was connecting find and pulling environment variables from my .env file, however my Next.Js app was pulling database connection parameters from my .env.local file. I needed to double check exactly which database I was connecting to.

Upvotes: 0

Steven Linus
Steven Linus

Reputation: 1

I guess the field name you defined does not match your inputs. I just came across the same error. It turns out the field names do not match.....

Upvotes: 0

Luciano Balbiano
Luciano Balbiano

Reputation: 51

I found out what is wrong here, "TypeError: The argument "payload" must be of type object. Received null" is always an error in the json we send in Postman/thunder. In this case, I have a property called "maintenance_date" which I defined as "timestamp", so I have to pass it the full format of a "timestamp" otherwise it will fail. enter image description here

enter image description here

Upvotes: 0

fisio
fisio

Reputation: 554

likely prisma is not correctly connecting to db. make sure:

  • db is running
  • schema is up to date: npx prisma migrate

Upvotes: 1

Related Questions