본문 바로가기

Backend/NestJS

(47)
클라이언트에서 영문 쿼리 백엔드단에서 한글로 받는 방법 클라이언트에서 영문으로 쿼리명을 넘겨주고, 서버단에서는 db에 해당 이름이 한글로 저장되어 있는 경우가 있었다. 이런 경우에는 서버에서 어떻게 처리하면 좋을까? 예를 들어 클라이언트에서 banana를 쿼리로 넘겨준다면, 서버단에서는 db에 해당 값이 한글로 저장되어 있으므로 한글로 변환이 필요하다. * 기존에는 서버단 > 클라이언트단으로 쿼리를 보낼 때 한글이름으로 보냈었는데, 클라이언트단에서 utf-8 인코딩 에러가 발생하는 바람에 쿼리에서 해당 단어가 깨져서 나오는 현상이 있었다. 일단 클라이언트에서 넘어오는 한글 이름을 enum으로 받자. export const GangVoteGradeToKo: { [index:string]: string } = { banana: '바나나', apple: '사과' ..
2023-11-30과 같이 컬럼 내에서 Date가 나오게 하려면 entity 파일에서 Column을 다음과 같이 작성해주면 된다. (in postgreSQL) @Column({ type: 'date' }) @Trnasform(transformDate) date: Date; date와 관련해서 entity에서 어떻게 정의하고, post 요청을 날릴때 계속 date는 instance여야 한다고 하는데, 여간 고민이 많다.. ㅜㅜ
timestamp 값이 내려가지 않았던 문제 수정 문제 상황 db 테이블에는 timestamp 컬럼에 값이 존재했는데, timestamp 값이 계속해서 null로 내려가는 문제가 있었다. responseData.data.metadata.attributes.forEach((attribute) => { if (attribute.value === 'Used') { const findHistory = benefitTxHistoryList.find((history) => history.traitType === attribute.type); if (findHistory) { attribute.timestamp = findHistory.timestamp; } else { attribute.timestamp = null; } } else { attribute.time..
호출한 API의 data 찍어보는법 const response = await firstValueFrom(this.httpService.get(`${this.configService.get('디렉토리이름.url')}/디렉토리이름/경로1/경로2`)); const responseData = response.data; return responseData; 이런 식으로 호출한 API의 데이터를 찍어볼 수 있다.
If BenefitTxHistoryRepository is a provider, is it part of the current AdminApiModule? 문제 상황 [Nest] 22390 - 10/26/2023, 8:58:32 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the AdminApiService (?). Please make sure that the argument BenefitTxHistoryRepository at index [0] is available in the AdminApiModule context. Potential solutions: - Is AdminApiModule a valid NestJS module? - If BenefitTxHistoryRepository is a provider, is it part of the current AdminApiModul..
param.ts 파일이나 dto에서 다른 파일에서 쓰여진 Enum 끌고오기 param.ts 파일에서 기존에 작성했던 Params에 다른 파일에서 작성한 Enum을 끌고와야 할 일이 있었다. 그래서 이 방법을 어떻게 해야하나 고민하던 중 해결할 수 있었다. 기존 방식 export class CompanyRequestParams extends CollectionAddressOrSlugParams { @Type(() => Number) @IsNumber() @IsOptional() readonly tokenId: number; @IsString() @IsOptional() readonly walletAddress: string; // 여기에 Enum 파일 끌고올 것 } 그럼 enum에서 선언된 @Enum 어노테이션을 통해 가져와야 하지 않겠는가. @Enum('benefitType')..
must be a number conforming to the specified constraints params.ts에 담아뒀던 파일(dto도 가능)에 다음과 같이 정의를 했었다. 해결 전 export class CompanyRequestParams extends CollectionAddressOrSlugParams { @IsNumber() @IsOptional() readonly tokenId: number; ... } Run을 해보니 must be a number conforming to the specified constraints 라는 400 에러가 떴다. number Type에 대해 뭔가 문제가 있는걸까 싶어서 구글링을 해보니 type 변환의 문제였으며 @Type 데코레이터로 Number로 type을 변환해줘야 했다. 해결 후 export class CompanyRequestParams ext..
keyof와 typeof 파일을 둘러보다보니, 다음과 같은 코드가 있었다. export const ActivityType = { ... } as const; export type ActivityType = (typeof ActivityType)[keyof typeof ActivityType]; 도대체 typeof와 keyof가 무엇이냐 말이다! 결론적으론, ActivityType 내부에 쓰여진 항목들을 다른 파일에서 import 해와서 잘 쓸 수 있다는 것. 일단 정의를 보자면, typeof : 객체 데이터를 객체 타입으로 변환해주는 연산자 객체는, 객체 자체를 type으로 쓸 수 없는데 객체에 쓰인 type 구조를 그대로 가져와 독립된 타입으로 만들어 사용하고 싶을때 사용한다 * class는 class 자체가 객체 type이 ..
다른 api 호출해오는법 (feat. httpService) 내가 작성한 api가 다른 곳에서 작성된 api를 필요로 할 때가 있다. 그때 바로 사용할 수 있는 것이 httpService이다. import { HttpService } from 'nestjs/axios'; nestjs의 axios 라이브러리에 HttpService가 존재한다. 이를 사용하기 위해서는 constructor에 아래와 같이 주입을 해주어야 한다 private readonly httpService: HttpService, 대체적으로, 이런 형태를 띈다. const 변수명 = await firstValueFrom(this.httpService.get(`${this.configService.get('url의 제일 루트 디렉토리 경로')}/해당 url의 디렉토리 경로/api 이름`)); firs..
api.module.ts에서 global:true로 넣기 @Module({ imports: [ CommonModule, ScheduleModule.forRoot(), { module: ChampionDataModule, global: true, }, { module: AuthModule, global: true, }, ... 일일이 module.ts 파일에서 정의를 해주지 않아도 global:true 옵션에서 해당 모듈을 모두 가져올 수 있다고 한다. 다만 module : 모듈 이름 에서 모듈 이름과 관련된 파일이 정의되어 있어야 한다. 예를 들면 ChampionDataModule에서 champion-data.module.ts 파일이 정의되어 있어야 하고 Auth 모듈도 auth.module.ts 파일이 정의되어 있어야 한다.