2017-08-31 26 views
5

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

有人能看到我在做什麼錯在這裏?

P.S.以下是錯誤消息我是從服務器取回,其中獲得來自console.log("Got an error logging in, here's the message: ", err);登錄'S:

"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":"[email protected]","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":"[email protected]","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":"[email protected]","password":"dddddd"}', 
      _bodyText: '{"email":"[email protected]","password":"dddddd"}', 
      bodyUsed: true } } } 

這裏是什麼樣子的Android模擬器(仿效三星Galaxy S7)的截圖:

Yellow error bar showing "Possible Unhandled Promise Rejection..."

+0

它可以簡單地是'err'或'err.response'是不確定的,因此嘗試登錄'err.response.data'將拋出一個錯誤,因此,即使您正在使用'.catch' - 因爲它拋出一個錯誤,你有一個未處理的拒絕在你手上 –

+0

我已經記錄err.response.data - 我甚至將它包含在SO問題中。它從服務器正確返回錯誤。 @JaromandaX –

+0

在代碼中你清楚地說明了'當我得到一個錯誤時遇到大問題' - 你從不說什麼'console.log('...',err.response.data)'outputs - 你顯示錯誤信息你從服務器獲取,但不能是'err.response.data' - 看起來像它不僅僅是'err.response.data' –

回答

3

沒有什麼錯在這個片段。

  • 你發送一個請求。
  • 您收到401 - 未經授權的回覆。
  • Catch收到錯誤並記錄下來。

有趣的部分是爲什麼你會得到未處理的承諾拒絕錯誤。 但是這是拋出其他地方。所以你需要提供更多的代碼。

編輯: 也許你只需要來回報您的愛可信承諾調用函數?

+0

很酷。我會在今晚提供更多的代碼/信息。希望我找出問題的真正根源。否則我會接受這個作爲+150代表比賽最後一天的答案。由於 –

+0

我當然會很高興得到了代表,但我並沒有解決您的問題; O) –

相關問題