• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

existsByEmail 에서 Type '{_id: any; }' is not assignable to type 'boolean'. 에러 발생

22.09.12 15:01 작성 조회수 1.2k

3

@Injectable()
export class CatsRepository {
  constructor(@InjectModel(Cat.name) private readonly catModel: Model<Cat>) {}

  async existsByEmail(email: string): Promise<boolean> {
    try {
      const result = await this.catModel.exists({ email });
      return result;
    } catch (error) {
      throw new HttpException('db error', 400);
    }
  }
}

Type '{_id: any; }' is not assignable to type 'boolean'. 에러가 뜹니다.
여기서 if (result) return result else false 를 하라고 하셨는데, 제가 문법을 잘 모르겠어서 어떻게 써야할 지 모르겠어요... 알려주시면 안될까요? 여기서 막혀서 진도를 못나가요.

답변 4

·

답변을 작성해보세요.

6

Young님의 프로필

Young

2022.12.26

async existsByEmail(email: string): Promise<boolean> {
    try {
      const result = await this.catModel.exists({ email });
      if (result) return true;
      else return false;
    }
    catch (error) {
      throw new HttpException('db error', 400);
    }
  }

위와 같이 작성하세요

4

호잇님의 프로필

호잇

2022.09.14

https://mongoosejs.com/docs/migrating_to_6.html#model-exists-returns-a-lean-document-instead-of-boolean

mongoose v6 이상에서는 exist()에서 boolean 값이 아닌 _id 값을 return 해주는 것 같습니다.

0

김건탁님의 프로필

김건탁

2022.09.13

안녕하세요 bugi님.작성해주신 질문에 대한 답변 남깁니다.

 

const result = await this.catModel.exists({ email });
return result as boolean;

위 코드와 같이 작성하셔도 똑같은 에러가 발생하실까요?

bugi님의 프로필

bugi

질문자

2022.09.15

넵 retrun result as boolean; 부분이 syntax에러가 뜹니다.

0

bugi님의 프로필

bugi

질문자

2022.09.12

async existsByEmail(email: string): Promise<boolean> {
    try {
      const result = await this.catModel.exists({ email });
      return !!result;
    } catch (error) {
      throw new HttpException('db error', 400);
    }
  }


이렇게 바꿔도 되는건가요?? 예시가 뭔가요?