inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

Repository 패턴과 레이어 분리

@InjectModel의 필요성

272

sanghyub hyun

작성한 질문수 3

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만 안되었을까요?? 

 

 

express mongodb nodejs ssr NestJS

답변 1

0

윤상석

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


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

프로젝트 환경 세팅할 때 최신 노드 버젼을 사용하시는 분들은 참고하셔도 좋을 것 같아요~

2

100

1

DTO에 대한 질문

1

97

2

백엔드 MVC에서 View의 역할은 무엇인가요?

1

111

2

추가 업데이트 관련 건

0

105

2

nest js 버전문제

0

95

2

mongdb 스키마 공식 문서와 형태가 다른 이유 궁금합니다.

0

111

1

라인 끝에 에러 표시(eslint) 때문에 구글 찾아 보니.

0

83

1

전체 고양이 조회 라우터 중 error.message 오류

0

78

1

캡슐화 추가 설명 중 단일책임원칙 관련 질문

0

115

0

42강 고양이끼리 소통 댓글 구현 중 Schema hasn't been registered for model 'comments' 에러 해결

0

86

1

채팅 이슈

0

137

1

모듈이 더 이상 지원하지 않는답니다

0

215

1

오류가 있습니다

0

114

1

import 에서 오류가 납니다

0

131

1

이런 오류가 나옵니다

0

106

1

에러가 발생합니다

0

117

1

프론트 에러 뜨는데 수정 안해주시나요

0

166

1

emit() broadcast.emit() 질문있습니다

0

107

1

서버연결이 안됩니다.

1

408

1

[PM2][ERROR] Command not found

0

528

1

S3에 업로드까지는 성공했는데 사진이 나오지 않습니다.

0

254

1

error_code : Property 'user' does not exist on type 'Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>'.ts(2339)

0

607

1

jwt를 따로 연습하고 있는데 env를 못읽는 것 같습니다.

0

330

2

Ec2로 안하시는 이유가 있을까요?

0

345

1