• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

500 에러 발생

22.01.13 16:38 작성 조회수 193

1

안녕하세요

payload의 sub를 넘겨 레포지토리에서 함수를 실행하는 부분이 자꾸 500에러가 뜹니다.

ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'findUserByIdWithoutPwd')

TypeError: Cannot read properties of undefined (reading 'findUserByIdWithoutPwd')

로 프로퍼티 undefined 오류가 발생하는데 어디를 확인해 봐야 좋을까요? 우선 startegy 파일과 repository 파일, Payload 파일에서 관련된 코드는 아래와 같습니다.

 

async validate(payload: Payload) {
    const user = await this.userRepository.findUserByIdWithoutPwd(payload.sub);

    if (user) {
      return user; // request.user안에 user가 들어감
    } else {
      throw new UnauthorizedException('접근 오류입니다');
    }
  }
  async findUserByIdWithoutPwd(subId: string): Promise<User | null> {
    const user = await this.userModel.findById(subId).select('-password');
    return user;
  }
export type Payload = {
  id: string;
  sub: string;
};

   아 그리고 저는 email이 아닌 id로 회원가입 서비스를 만들어서 서브를 아래처럼 받았는데 그래서 오류가 난 걸까요?

 

    const payload = { id: id, sub: user._id };
    return {
      token: this.jwtService.sign(payload),
    };

답변 1

답변을 작성해보세요.

4

bee님의 프로필

bee

질문자

2022.01.13

거의 두 시간을 버렸는데 auth.service에 @Injectable()을 달지 않아서 생긴 오류였습니다... 혹시 추후 저처럼 에러 발생하시는 분들은 DI 확인해 보세요 ㅎ...