inflearn logo
강의

講義

知識共有

しっかりしたバックエンドNestJS、基礎から深化まで

リポジトリパターンとレイヤーの分離

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

1536

bugi

投稿した質問数 4

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 를 하라고 하셨는데, 제가 문법을 잘 모르겠어서 어떻게 써야할 지 모르겠어요... 알려주시면 안될까요? 여기서 막혀서 진도를 못나가요.

mongodb nodejs express NestJS ssr

回答 4

6

Young

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

mjgo7283

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

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

0

caput3545

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

 

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

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

0

bugi

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

0

bugi

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


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

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

2

80

1

DTO에 대한 질문

1

87

2

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

1

93

2

추가 업데이트 관련 건

0

90

2

nest js 버전문제

0

79

2

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

0

103

1

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

0

74

1

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

0

67

1

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

0

104

0

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

0

82

1

채팅 이슈

0

133

1

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

0

206

1

오류가 있습니다

0

106

1

import 에서 오류가 납니다

0

127

1

이런 오류가 나옵니다

0

100

1

에러가 발생합니다

0

109

1

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

0

156

1

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

0

101

1

서버연결이 안됩니다.

1

403

1

[PM2][ERROR] Command not found

0

520

1

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

0

248

1

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

0

601

1

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

0

322

2

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

0

343

1