在我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)的截圖:
它可以簡單地是'err'或'err.response'是不確定的,因此嘗試登錄'err.response.data'將拋出一個錯誤,因此,即使您正在使用'.catch' - 因爲它拋出一個錯誤,你有一個未處理的拒絕在你手上 –
我已經記錄err.response.data - 我甚至將它包含在SO問題中。它從服務器正確返回錯誤。 @JaromandaX –
在代碼中你清楚地說明了'當我得到一個錯誤時遇到大問題' - 你從不說什麼'console.log('...',err.response.data)'outputs - 你顯示錯誤信息你從服務器獲取,但不能是'err.response.data' - 看起來像它不僅僅是'err.response.data' –