* 문제 :
* 해결 : DTO
export class CreateBoardDto{
title:string;
description:string;
}
* 컨트롤러
//전
@Post()
createBoard( @Body('title') title:string, @Body('description') description:string, ) : Board{
return this.boardsService.createBoard(title,description);
}
//후
//생성은 post
@Post()
createBoard( @Body() createBoardDto : CreateBoardDto) : Board{
return this.boardsService.createBoard(createBoardDto);
}
@Body() 뒤에 Dto를 매개변수로 두면된다.
* 서비스
createBoard(createBoardDto : CreateBoardDto){
const {title,description} = createBoardDto;
const board = {
id : uuid(), //자동으로 유니크한 값을 넣어줌
title : title,
description : description,
status : BoardStatus.PUBLIC
}
this.boards.push(board);
return board;
}
맨앞줄에서 dto의 변수를 대입하면된다.
'JS > Nest.js' 카테고리의 다른 글
[Nest JS] 없는 게시물 찾을때 예외처리 (0) | 2024.08.17 |
---|---|
[Nest JS] 파이프 for 유효성체크 (0) | 2024.08.17 |
[Nest JS] Update 구현 (0) | 2024.08.17 |
[Nest JS] ID로 특정 게시물 가져오기 / 지우기 (0) | 2024.08.17 |
[Nest JS] 설정 ~ 게시글 생성 (0) | 2024.08.17 |