skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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


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

    • db is running
    • schema is up to date: npx prisma migrate
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search