簡體   English   中英

可能未處理的承諾拒絕(Id:0):TypeError:undefined不是對象(評估'err.response.data')

[英]Possible Unhandled Promise Rejection (Id: 0): TypeError: undefined is not an object (evaluating 'err.response.data')

我有一個克隆的React Native應用程序。 它使用Node.js APIMongoDB連接。 但是,在注冊到應用程序后,它無法插入或從MongoDB獲取數據。 另外,在我的模擬器上,屏幕底部出現黃色錯誤,上面寫着:

可能的未處理的Promise拒絕(id:0):TypeError:undefined不是對象(評估'err.response.data')

這是我的代碼:

export const loginuser = userData => dispatch => {
    axios.post("http://localhost:4000/api/users/login", userData)
      .then(res => {

        // save user token to local storage 
        const { token } = res.data;

        AsyncStorage.setItem("jwtToken", token);
        console.log(AsyncStorage.setItem())

        // set token to auth header i.e authorization 
        setAuthToken(token);

        // decode the token and saveuser to deoded

        const decoded = jwt_decode(token);
        console.log(token)
        //set current user 

        console.log(decoded)
        dispatch(setCurrentUser(decoded));

        Actions.main()
      })
      .catch(err => dispatch({
        type:GET_ERRORS,
        payload: err.response.data
      })
      )
  }

我可以知道解決此錯誤的最佳解決方案。 希望得到你的幫助。 謝謝

請查看Axios文檔,了解如何處理錯誤: https//github.com/axios/axios#handling-errors

err.response可能為空:

axios.post("http://localhost:4000/api/users/login", userData)
  .catch(function (error) {
    if (error.response) {
      // The request was made and the server responded with a status code
      // that falls out of the range of 2xx
      console.log(error.response.data);
      console.log(error.response.status);
      console.log(error.response.headers);
    } else if (error.request) {
      // The request was made but no response was received
      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
      // http.ClientRequest in node.js
      console.log(error.request);
    } else {
      // Something happened in setting up the request that triggered an Error
      console.log('Error', error.message);
    }
    console.log(error.config);
  });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM