값을 변환하고 문서를 저장하기 위해 DTO 파일을 만들려고합니다.
export class CreateProductDto {
readonly pricing: {
readonly list: number;
}
}
async create(@Body() createProductDto: CreateProductDto) {
console.log(createProductDto);
console.log(createProductDto.pricing.list);
}
import * as mongoose from 'mongoose';
export const ProductSchema = new mongoose.Schema({
pricing: {
list: {
type: Number,
},
},
});
그러나 princing.list의 값은 정의되지 않았습니다 .
NestJS에서 올바른 방법은 무엇입니까?
귀하의 dto가 될 경우이 문서 를 살펴보십시오.
import { IsNumber, IsObject } from 'class-validator';
import { Type } from 'class-transformer';
export class ListDto {
@IsNumber()
readonly list: number
}
export class CreateProductDto {
@IsObject()
@ValidateNested() @Type(() => ListDto)
readonly pricing: ListDto
}
당신의 main.ts에서
import { ValidationPipe } from '@nestjs/common';
app.useGlobalPipes(new ValidationPipe());
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다