강의

멘토링

커뮤니티

인프런 커뮤니티 질문&답변

sanghyub hyun님의 프로필 이미지
sanghyub hyun

작성한 질문수

탄탄한 백엔드 NestJS, 기초부터 심화까지

Repository 패턴과 레이어 분리

@InjectModel의 필요성

작성

·

267

1

안녕하세요 강사님 

 

실습을 진행하는 도중에 에러를 만나 해결하는 과정에서 발생한 궁금증을 질문드립니다. 

 

기존에 아래와 같이 repository에 접근하는 service단에서 코드가 에러가 발생했습니다. 

 

//users.service.ts 

async signUp(body: UsersCreateDto) {
    const { email, name, password, passwordConfirm, imgUrl, role } = body;
    //duplicated email
    console.log({ email, name, password, passwordConfirm, imgUrl, role });
    const isUserExist = await this.usersRepository.existsByEmail(email);

 ....

}

다른 함수인 create는 접근이 가능했는데 existByEmail만 접근이 되지 않았습니다. 

//users.repository.ts



@Injectable()
export class UsersRepository {
  constructor(
    @InjectModel(User.name) private readonly userModel: Model<User>,
  ) {}

  async existsByEmail(email: string): Promise<boolean> {
    const result = (await this.userModel.exists({ email })) ? true : false;
    const result = true;
    return result;
  }

  async create(user: UsersDbInsertDto): Promise<User> {
    try {
      const result = await this.userModel.create({ user });
      return result;
    } catch (error) {
      throw new HttpException('db error', 400);
    }
  }
}

 

 

구글링을 하며 찾아본 결과 아래 링크에서 답을 찾아서 타이포 에러는 해결했는데 원리를 모르곘습니다. 

 

https://stackoverflow.com/a/61396554

 

왜 @InjectModel을 삭제했을 때 repository함수에 접근할 수 없었을까요? 왜 다른 함수(create) 는 접근이 되고 existByEmail만 안되었을까요?? 

 

 

답변 1

0

윤상석님의 프로필 이미지
윤상석
지식공유자

인녕하세요! 아래 부분에서 문법 에러가 발생하지 않으셨을까요???


async existsByEmail(email: string): Promise<boolean> { const result = (await this.userModel.exists({ email })) ? true : false; const result = true; return result; }
sanghyub hyun님의 프로필 이미지
sanghyub hyun

작성한 질문수

질문하기