繁体   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