728x90
반응형
먼저 다음과 같이 main.ts 파일에 파이프를 추가해준다.
main.ts
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(
new ValidationPipe({
whitelist : true,
forbidNonWhitelisted : true,
transform : true
})
)
await app.listen(3000);
}
bootstrap();
그다음 controller와 service에 GET메소드로 getAll() 메소드를 만들어보자.
cats.controller.ts
import { Controller, Get, Param, Post, Delete, Patch, Body, Query } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';
import { Cat } from './schemas/cat.schema';
@Controller('cats')
export class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get()
async getAll(): Promise<Cat[]> {
return await this.catsService.getAll();
}
}
cats.service.ts
import { Model } from 'mongoose';
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Cat, CatDocument } from './schemas/cat.schema';
import { CreateCatDto } from './dto/create-cat.dto';
@Injectable()
export class CatsService {
constructor(@InjectModel(Cat.name) private catModel: Model<CatDocument>) {}
async getAll(): Promise<Cat[]> {
return await this.catModel.find().exec();
}
}
postman으로 확인해보자
아직 추가한 Document가 없기 때문에 당연히 빈배열로 받아와진다.
728x90
반응형
'Back-End > Nest.js' 카테고리의 다른 글
Nest.js | MongoDB | PATCH and DELETE (0) | 2021.10.06 |
---|---|
Nest.js | MongoDB | Create (0) | 2021.10.06 |
Nest.js | MongoDB | Model,Schema,Controller (0) | 2021.10.06 |
Nest.js | MongoDB | Schema (0) | 2021.10.06 |
Nest.js | E2E TESTING | PATCH and DELETE (0) | 2021.09.30 |