[Nest.js] ERROR [ExceptionHandler] metatype is not a constructor

Jae ·2022년 5월 24일
4

ERROR Handling

목록 보기
2/2

문제


nest.js 서버 실행 시 TypeError: metatype is not a constructor 에러가 발생했다. 어디서 발생한 문제인지 도저히 알 수 없어서 매우 난감했다.

원인

@UseGuards(AuthGuard) 메서드가 원인인데
@UseGuards(AuthGuard()) 바보같이 함수 형태로 작성하지 않았다.

그런데 수정하고 다시 실행하면 아래의 오류가 뜬다.

아래처럼 모듈 안에 PassportModule.register({ defaultStrategy: 'jwt' }) 을 같이 작성하지 않았기 때문...

해결

토큰 인증을 위한 로직을 내가 작성한 부분이 아니라 잘 이해하지 못했지만, 오류를 통해서 @UseGuards(AuthGuard()) 메서드 적용되는 로직의 흐름을 대략적으로 이해할 수 있었다.

  1. 토큰 인증을 통한 API 실행을 하기 위해선, 모듈 파일에서 패스포트 모듈을 import 하고, PassportModule.register({ defaultStrategy: 'jwt' }) 코드를 작성해줘야 한다.

  2. CRUD를 위한 API를 작성하기 전 @UseGuards(AuthGuard()) 가 오탈자 없이 잘 적용이 되어있는지 확인하자.

Reference

https://github.com/nestjs/nest/issues/2399

5개의 댓글

comment-user-thumbnail
2023년 2월 25일

복받으세요,,

답글 달기
comment-user-thumbnail
2023년 3월 3일

복받으세요,,22

답글 달기
comment-user-thumbnail
2023년 3월 25일

복받으세요,,333

답글 달기
comment-user-thumbnail
2023년 5월 24일

복받으세요,,4444

답글 달기

you saved my life!

답글 달기