在我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' –