React Native 오류 해결 - WARN Possible Unhandled Promise Rejection (id: 0)

guddls ju·2023년 1월 12일
0

React Native 오류

목록 보기
1/1

오늘 마주친 에러는 "Possible Unhandled Promise Rejection" 되시겠다.

원인

axios를 사용해 api를 호출할 때 catch로 예외 처리를 하지 않아서.

예시)

loadData: async (date, token) => {
    const { year, month, day } = DateSplit(date);
    const res = await axios.post(
      hospitalLoadPath,
      { year: year, month: month + 1, date: day },
      {
        headers: {
          authorization: token,
        },
      },
    );

    if (res.status === 200 && res.data.result === 1) {
      console.log(res);
      set(() => ({ dailyData: res.data.data }));
      return true;
    } else return false;
  },

해결 방법

try catch 사용해서 예외처리.

예시)

  loadData: async (date, token) => {
    const { year, month, day } = DateSplit(date);
 
    try {
      const res = await axios.post(
        hospitalLoadPath,
        { year: year, month: month + 1, date: day },
        {
          headers: {
            authorization: token,
          },
        },
      );
      if (res.status === 200 && res.data.result === 1) {
        console.log(res);
        set(() => ({ dailyData: res.data.data }));
        return true;
      } else return false;
    } catch (err) {
      console.log('err', err);
    }
profile
효율에 미친자

0개의 댓글