-
Nestjs ConfigModule 사용시 환경변수 boolean값을 가져올때 string으로 가져오는 문제Nest.js 2024. 6. 24. 14:09728x90
let cookieOptions: CookieOptions = { maxAge: Number(this.configService.get<number>(ENV_COOKIE_MAX_AGE)), secure: this.configService.get(ENV_COOKIE_SECURE), };
CookieOptions에서 secure 프로퍼티의 옵션에는 boolean 값만 올 수 있는데
typeof this.configService.get('COOKIE_SECURE')
typeof this.configService.get(ENV_COOKIE_SECURE)
> this.configService.get으로 .env 에 true나 false값을 가져오면
***cookie_secure=*** string
***cookie_secure2=*** stringstring 타입을 가져오는것을 알 수 있다. 그래서 런타임 환경에서 에러가 발생해버린다.
이를 해결 하기 위해서 환경변수 boolean 값을 가져올때에는 JSON.parse를 이용한다.let cookieOptions: CookieOptions = { maxAge: Number(this.configService.get<number>(ENV_COOKIE_MAX_AGE)), secure: JSON.parse(this.configService.get(ENV_COOKIE_SECURE)!), };
typeof JSON.parse(this.configService.get(ENV_COOKIE_SECURE)!)
***secure***= boolean728x90'Nest.js' 카테고리의 다른 글
NestJS class-transformer TypeOrm 변환 관련 LifeCycle (0) 2024.07.07 prisma 스키마 관련 라이브러리 (0) 2024.06.28 메일 전송시 Promise.allSettled 사용하기 (0) 2024.06.08 NestJS에서 repository의 insert메서드로 유동적으로 데이터 생성하기 (2) 2024.06.04 NestJS Response Dto에 generic을 swagger ApiProperty 데코레이터에 사용하기 (1) 2024.06.04