[React] Uncaught TypeError: Cannot read properties of undefined (reading ... )

👩‍💻💖·2022년 8월 5일
0

TroubleShooting

목록 보기
1/3

1. 오류

Uncaught TypeError: Cannot read properties of undefined (reading 'text')

위와 같이 오류가 노출었는데 BucketDetail.js의 27번째 줄에서 오류가 발생되었다.


if (bucket_obj[0].text.trim() === "") {
    bucket_text = "";
  } else {
    bucket_text = bucket_obj[0].text;
  }

2. 원인

bucket_obj[0] 자체가 값이 없어서 오류가 발생, 삭제를 진행하기 위해 삭제 진행 후 history.goBack() 함수를 넣었으나,

3. 해결

옵셔널 체이닝(OptionalChaining)을 이용하여, return 시켜 줬더니 Tada! 해결되었다.!

const bucket_text = bucket_obj[0]?.text;

0개의 댓글