Object is possibly 'null'.ts(2531)

Jaymee·2021년 9월 22일
0

Object is possibly 'null'.ts(2531)
객체의 타입이 null 일 수도 있다는 뜻이다.

const user = await findUserById({userId}) 
const result = await bcrypt.compare(password, user.password);

user.password 에서 user의 값이 null일 수도 있기 때문에 생겨난 문제다.


에러 발생

const user: (IUser & Document<any, any, any> & {
    _id: any;
}) | null

IUser(interface dto) 일 수도 있지만 Null 일 가능도 있다~ 라는 뜻

구글링 하니까 user?.password 로 해결 가능하다고 하는데, 흠... 안됨..

그래서 결국은 null인 케이스도 처리해줬다.

if(!user){
  ~~~
}

https://stackoverflow.com/questions/49431880/ts2531-object-is-possibly-null

profile
backend developer

0개의 댓글