강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của wlgns4102353
wlgns4102353

câu hỏi đã được viết

NestJS phụ trợ vững chắc, từ cơ bản đến nâng cao

Mẫu kho lưu trữ và tách lớp

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

Viết

·

1.5K

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

mongodbnodejsexpressNestJSssr

Câu trả lời 4

6

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

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

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

0

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

 

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

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

bugi님의 프로필 이미지
bugi
Người đặt câu hỏi

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

0

bugi님의 프로필 이미지
bugi
Người đặt câu hỏi

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


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

Hình ảnh hồ sơ của wlgns4102353
wlgns4102353

câu hỏi đã được viết

Đặt câu hỏi