簡體   English   中英

Axios 承諾處理 - 在 react-native 中獲取“可能未處理的承諾拒絕 - 類型錯誤:網絡請求失敗”

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

在登錄屏幕上的react-native應用程序中,我正在努力在輸入錯誤的用戶名/密碼組合后為用戶提供很好的錯誤消息。 為了與 API 交互,我使用了庫Axios 但是,當我在catch語句中遇到錯誤時,我會收到一條丑陋的錯誤消息,說我有一個“未處理的承諾拒絕”,並且我無法執行諸如設置組件狀態或導航到新頁面之類的操作。

我看不出我做錯了什么,它看起來和我在文檔中看到的例子一模一樣。

在我的表單提交功能中,我有:

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);
});

誰能看到我在這里做錯了什么?

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 } } }

這是在 Android 模擬器(模擬三星 Galaxy S7)上的屏幕截圖:

黃色錯誤欄顯示“可能未處理的承諾拒絕......”

這個片段沒有任何問題。

  • 您發送請求。
  • 您會收到 401 - 未經授權的響應。
  • Catch 收到錯誤並記錄下來。

有趣的部分是為什么您會收到未處理的承諾拒絕錯誤。 但這被扔到了其他地方。 所以你需要提供更多的代碼。

編輯:也許您只需要將 axios 承諾返回給調用函數?

我有同樣的問題,我使用 Firebase 作為數據庫

我解決了這個問題將數據庫放在實時而不是像圖片一樣的雲 Firestore

將數據庫放在實時而不是像圖片一樣的雲 Firestore

我希望這對你有用

暫無
暫無

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

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