* 에러

Network error
..
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:162:14 in _callReactNativeMicrotasksPass
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:413:41 in callReactNativeMicrotasks
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:391:6 in __callReactNativeMicrotasks
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:133:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:368:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:132:4 in flushedQueue

 

* 환경

react native expo + Django

 

* 해결

- backend에 settings.py 변경

  expo start를 했을 때 연결되는 expo ip주소를 넣어주면 된다.

ALLOWED_HOSTS = ['자신의 ip주소'] 
# ex) ALLOWED_HOSTS = ['192.168.0.12']

- backend 실행 시 코드

  자신의 ip:port 로 변경

python manage.py runserver 192.168.0.12:8080

- frontend axios 부분 코드

 button의 onPress 함수가 onPressLogin

const onPressLogin = async () => {
    if (id == "" || pw == "") {
      alert("아이디와 비밀번호를 입력해 주세요");
    } else {
      const data = {
        username: id,
        password: pw,
      };
      console.log(data);

      try {
        const response = await axios
          .post(`http://192.168.0.12:8080/api/v1/token`, data)
          .then(function async(response) {
            if (response.data["success"] == true) {
              setIdNum(response.data["id"]);
              alert("로그인되었습니다.");
              navigation.navigate("Home");
              setId("");
              setPw("");
            }
          })
          .catch(function (error) {
            alert("로그인 오류입니다.");
            //console.log(error.response.data);
            console.log(error);
            throw error;
          });
      } catch (error) {
        console.log(error);
        throw error;
      }
    }
  };

 

※ stackoverflow 참고

https://stackoverflow.com/questions/62031415/unhandled-promise-rejection-typeerror-network-request-failed-expo-node-backend

 

Unhandled promise rejection: TypeError: Network request failed expo node backend

I have a node backend that my expo app is querying. The node-express-mongo backend works just perfect which I can verify using GET Request from Postman but I get unhandled promise rejection Network

stackoverflow.com

 

728x90

+ Recent posts