简体   繁体   English

Axios 承诺处理 - 在 react-native 中获取“可能未处理的承诺拒绝 - 类型错误:网络请求失败”

[英]Axios Promise Handling - Getting “Possible Unhandled Promise Rejection - TypeError: Network request failed” in react-native

In my react-native application on the login screen I'm working on providing the user with nice error messaging after entering in the incorrect username / password combination.在登录屏幕上的react-native应用程序中,我正在努力在输入错误的用户名/密码组合后为用户提供很好的错误消息。 To interact with the API I'm using the library Axios .为了与 API 交互,我使用了库Axios However when I get an error in the catch statement, I get this ugly error message saying that I have an "unhandled promise rejection" and I cannot do things such as set the components state or navigate to a new page.但是,当我在catch语句中遇到错误时,我会收到一条丑陋的错误消息,说我有一个“未处理的承诺拒绝”,并且我无法执行诸如设置组件状态或导航到新页面之类的操作。

I can't see what I'm doing wrong, it looks exactly like examples I've seen in the docs.我看不出我做错了什么,它看起来和我在文档中看到的例子一模一样。

In my form submission function I have:在我的表单提交功能中,我有:

axios.post('http://192.168.1.11:1337/login', {
  email: this.state.username,
  password: this.state.password
}).then(function (response) {

  // This stuff all seems to work great
  console.log("The response we got: ", response);
  if (response.status == 200) {
    console.log("Status code equals 200");
    Actions.homepage();
  }
}).catch(function (err) {

  // Run into big problems when I get an error
  console.log("Got an error logging in, here's the message: ", err);
});

Can anyone see what I'm doing wrong here?谁能看到我在这里做错了什么?

PS Here is the error message I'm getting back from the server, which get's logged from that console.log("Got an error logging in, here's the message: ", err); PS这是我从服务器返回的错误消息,该消息是从该console.log("Got an error logging in, here's the message: ", err); :

"Got an error logging in, here's the message:"

{ [Error: Request failed with status code 401]
  config: 
   { transformRequest: { '0': [Function: transformRequest] },
     transformResponse: { '0': [Function: transformResponse] },
     headers: 
      { Accept: 'application/json, text/plain, */*',
       'Content-Type': 'application/json;charset=utf-8' },
    timeout: 0,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    validateStatus: [Function: validateStatus],
    method: 'post',
    url: 'http://192.168.1.11:1337/login',
    data: '{"email":"zach@homies.io","password":"dddddd"}' },
  response: 
    { data: { message: 'Invalid password', user: false },
     status: 401,
     statusText: undefined,
     headers: 
     { map: 
        { connection: [ 'keep-alive' ],
          date: [ 'Thu, 31 Aug 2017 23:30:21 GMT' ],
          'x-powered-by': [ 'Sails <sailsjs.org>' ],
          vary: [ 'X-HTTP-Method-Override' ],
          'content-length': [ '52' ],
          'access-control-allow-credentials': [ '' ],
          'access-control-allow-origin': [ '' ],
          etag: [ 'W/"34-Ymi4isRxuJ6jE1EIS+AQag"' ],
          'access-control-allow-methods': [ '' ],
           'access-control-allow-headers': [ '' ],
           'access-control-expose-headers': [ '' ],
           'content-type': [ 'application/json; charset=utf-8' ] } },
     config: 
       { transformRequest: { '0': [Function: transformRequest] },
        transformResponse: { '0': [Function: transformResponse] },
        headers: 
         { Accept: 'application/json, text/plain, */*',
           'Content-Type': 'application/json;charset=utf-8' },
        timeout: 0,
        xsrfCookieName: 'XSRF-TOKEN',
        xsrfHeaderName: 'X-XSRF-TOKEN',
        maxContentLength: -1,
         validateStatus: [Function: validateStatus],
          method: 'post',
          url: 'http://192.168.1.11:1337/login',
          data: '{"email":"zach@homies.io","password":"dddddd"}' },
       request: 
        { url: 'http://192.168.1.11:1337/login',
          credentials: 'omit',
          headers: 
           { map: 
              { accept: [ 'application/json, text/plain, */*' ],
                'content-type': [ 'application/json;charset=utf-8' ] } },
          method: 'POST',
          mode: null,
          referrer: null,
          _bodyInit: '{"email":"zach@homies.io","password":"dddddd"}',
          _bodyText: '{"email":"zach@homies.io","password":"dddddd"}',
          bodyUsed: true } } }

Here is a screenshot of what it looks like on the Android emulator (emulating a Samsung Galaxy S7):这是在 Android 模拟器(模拟三星 Galaxy S7)上的屏幕截图:

黄色错误栏显示“可能未处理的承诺拒绝......”

There is nothing wrong in this snippet.这个片段没有任何问题。

  • You send a request.您发送请求。
  • You get a 401 - unauthorized response.您会收到 401 - 未经授权的响应。
  • Catch receives the error and logs it. Catch 收到错误并记录下来。

The interesting part is why you get that unhandled promise rejection error.有趣的部分是为什么您会收到未处理的承诺拒绝错误。 But this is thrown somewhere else.但这被扔到了其他地方。 So you need to provide more of the code.所以你需要提供更多的代码。

Edit: Perhaps you just have to return your axios promise to the calling function?编辑:也许您只需要将 axios 承诺返回给调用函数?

I have same problem and I use Firebase for database我有同样的问题,我使用 Firebase 作为数据库

I resolve this problem我解决了这个问题将数据库放在实时而不是像图片一样的云 Firestore

put database on realtime instead of cloud Firestore like the picture将数据库放在实时而不是像图片一样的云 Firestore

I hope this worked for you我希望这对你有用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native:可能未处理的承诺拒绝(id:0)TypeError:网络请求失败 - React Native: Possible unhandled promise rejection (id: 0) TypeError: Network request failed react-native 错误:[未处理的 promise 拒绝:错误:获取 Expo 令牌时遇到错误:TypeError:网络请求失败。] - react-native error:[Unhandled promise rejection: Error: Error encountered while fetching Expo token: TypeError: Network request failed.] 反应本地可能未处理的诺言拒绝 - Possible unhandled promise rejection in react-native 当我发出 Post 请求时,React Native 返回 [未处理的承诺拒绝:类型错误:网络请求失败] - React Native returns [Unhandled promise rejection: TypeError: Network request failed] when I'm making a Post Request React Native 获取错误:“[未处理的 promise 拒绝:TypeError:网络请求失败]” - React Native fetch error: “[Unhandled promise rejection: TypeError: Network request failed]” 可能未处理的承诺拒绝(ID:1):类型错误:网络请求失败 - Possible Unhandled Promise Rejection (id: 1): TypeError: Network re quest failed (本机)可能未处理的承诺拒绝FBSDK - (React-Native) Possible Unhandled Promise Rejection FBSDK 可能未处理的 Promise 拒绝 - React-Native with Firebase - Possible Unhandled Promise Rejection - React-Native with Firebase React Native Axios 未处理的承诺拒绝 - React Native Axios unhandled promise rejection React Native:可能的未处理的承诺拒绝(id:0):TypeError:undefined不是对象 - React Native: Possible Unhandled Promise Rejection (id: 0): TypeError: undefined is not an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM