카테고리 없음

에러 Cannot read properties of null (reading 'creatorId')TypeError: Cannot read properties of null (reading 'creatorId')

zkzk7290 2025. 1. 6. 03:07
반응형
Cannot read properties of null (reading 'creatorId') TypeError: Cannot read properties of null (reading 'creatorId')
번역하면
null 객체의 속성을 읽을 수 없습니다(reading 'creatorId').
TypeError: null 객체의 속성을 읽을 수 없습니다(reading 'creatorId').

 처음에는 400에러 떠서 콘솔에도 찍어보고  백에서 sout을 찍어보고 creatorId를 찍어  봤는데 값이 출력이 되지 않아 정말로 막막했다 그래서   AI에다가 프런트 전박적인 코들 주고 어떤 부분이  문제인지 물어봤는데  계속 내가 원하는 결과는 나오지 않았다 그래서 

 

onClick={() => {
            if (!vote) {
              console.error("Vote data is not loaded yet.");
              return;
            }
            handlePostVote(
              parseToNumGroupId,
              vote.creatorId,
              voteContent,
              createDate,
              closeDate
            );
          }}

 

이렇게 작성했다  

GPT에 도움 받아 작성한 코드인데 왜 값이 안나올까 생각해 보니깐! vote 객체 가 없으면 error 발생 그리고 리턴 

내가  실수 했구나 

그래서 수정한 코드가 

<button
          onClick={() =>{
            const creatorId = vote ? vote.creatorId : cookies.userId;
            handlePostVote(
              parseToNumGroupId,
              creatorId,
              voteContent,
              createDate,
              closeDate
            );
          }}
          css={closeModalButton}
        >
          등록
        </button>

그러니깐 

값이 나오지 안을때나 에러 400번대 에러인 경우에는  무조건 콘솔이나 백에서 출력을 해보자 

그러면 어디서 에러나 null인지를 대략 유추할 수 있다 

이번 에러 처럼!vote이런식의 코드 되어있는지 확인해보자  

반응형