0

在此处输入图像描述

我在 main.ts 中添加了以下代码

import { NestFactory } from '@nestjs/core';

import{SwaggerModule,DocumentBuilder} from '@nestjs/swagger'

import { AppModule } from './app.module';

async function bootstrap() {

  const app = await NestFactory.create(AppModule);
 
  const options = new DocumentBuilder()

    .setTitle('My API')

    .setDescription('API description')

    .setVersion('1.0')

    .build();

  const document = SwaggerModule.createDocument(app, options);

  SwaggerModule.setup('api', app, document);


  await app.listen(3000);

}
bootstrap();

//controller

@Post()

addProduct(

    @Body('title') title:string,
    @Body('price') price:number,


):any{

    const idFromService=this.productserive.addNewProduct(title,price);

    return idFromService;

}

//productservice

export class ProductService {

  products:Product[]=[]; 
 
    addNewProduct(title:string,price:number){

        const id=uId();

        const newproduct=new Product(id,title,price);

        this.products.push(newproduct);

        return {title,price};

    }

}
4

1 回答 1

0

// create a separate dto 
import { ApiProperty } from '@nestjs/swagger';

export class TestDto {
    @ApiProperty()
    readonly title: string

    @ApiProperty()
    readonly price: number
}

// use it in your controller
@Post()
    addProduct(@Body() TestDto:TestDto): any {
        return;
    }

于 2020-09-03T09:58:40.917 回答