Home [Error] console.log(Error) 객체 안나오는 현상
Post
Cancel

[Error] console.log(Error) 객체 안나오는 현상


Cannot destructure property ‘data’ of ‘(intermediate value)’ as it is undefined. 발생

원래 평상시에 위처럼 찍혀야 할 error 코드가 아래처럼 나오기 시작했다….
백에서 어떤 error인지 보내준 message 확인해야 하는데….

원인

응답이 올 떄 통신을 가로채는 코드가 문제였던 것 이다…
이미 진짜 error 코드는 여기서 쓰였다…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 응답 인터셉터 추가하기
api.interceptors.response.use(
  function (response) {
    // 응답 데이터가 있는 작업 수행
    return response;
  },
  function (error) {
    const {
      response: { status },
    } = error;
    // 응답 오류가 있는 작업 수행
    if (status === 401) {
      removeCookie();
      window.location.replace("/login");
      window.location.reload();
    }
  }
);
해결

Promise.reject(error)를 return 해주니 다시 정상적인 error 코드가 나온다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 응답 인터셉터 추가하기
api.interceptors.response.use(
  function (response) {
    // 응답 데이터가 있는 작업 수행
    return response;
  },
  function (error) {
    const {
      response: { status },
    } = error;
    // 응답 오류가 있는 작업 수행
    if (status === 401) {
      removeCookie();
      window.location.replace("/login");
      window.location.reload();
    }
    return Promise.reject(error);
  }
);

마치며

혹시 잘못된 정보나 궁금하신 게 있다면 편하게 댓글 달아주세요.
지적이나 피드백은 언제나 환영입니다.

This post is licensed under CC BY 4.0 by the author.