Search code examples
node.jstypescriptnestjs

NestJS Return Null in Body using DTO


I have a problem when I creating new resource with command nest g resource or using nest g module product the file already generated but the problem happen when I try to use POST request in the postman (my case is using hoppscotch and postman). but its return {} every time I connected to createProductDto using console log to check the request body is

Product Controller

import { Body, Controller, Post } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { CreateProductDto } from './dto/create-product.dto';
import { ProductService } from './product.service'

@Controller('api/products')
@ApiTags('products')
export class ProductController {
    constructor(private readonly productService: ProductService) { }

    @Post()
    async create(@Body() createProductDto: CreateProductDto) {
        console.log(createProductDto);
        return this.productService.create(createProductDto);
    }
}

Product Module

import { Module } from '@nestjs/common';
import { ProductService } from './product/product.service';
import { ProductController } from './product/product.controller';
import { PrismaModule } from 'src/prisma/prisma.module';

@Module({
  providers: [ProductService],
  controllers: [ProductController],
  imports: [PrismaModule]
})
export class ProductsModule { }

Create Product DTO

import { ApiProperty } from '@nestjs/swagger';

export class CreateProductDto {
    @ApiProperty()
    title: string;

    @ApiProperty({ required: false })
    description: string;

    @ApiProperty()
    body: string;

    @ApiProperty({ required: false, default: false })
    published?: boolean = false;
}

Product Service

import { Injectable } from '@nestjs/common';
import { CreateArticleDto } from 'src/articles/dto/create-article.dto';
import { PrismaService } from 'src/prisma/prisma.service';
import { CreateProductDto } from './dto/create-product.dto';

@Injectable()
export class ProductService {
    constructor(private prisma: PrismaService) { }

    create(createProductDto: CreateProductDto) {
        return this.prisma.product.create({ data: createProductDto });
    }
}

already try it 4 time already to generate new resource but its still the same, restart Pc and service still no change

{
  "name": "Amazing Product",
  "description": "This is a fantastic product you'll love!",
  "price": 10000,
  "image": "https://example.com/product-image.jpg", 
  "categoryId": "1"
}

but when I use @Body() createProductDto: any the request is available

when i submit the data i recieve error

  10 create(createProductDto: CreateProductDto) {
→ 11     return this.prisma.product.create({
           data: {
         +   name: String
           }
         })

Argument `name` is missing.

because the body request is always {}


Solution

  • Adding class-validator helps keep your data in check and avoids unexpected blanks in requests.

    This could really help solve the issue you're facing. If your data doesn't meet the expected format, you'll be notified directly, so you can quickly see where you need to make corrections.