gpt4 book ai didi

javascript - Axios Promise 处理 - 在 react-native 中获取 "Possible Unhandled Promise Rejection - TypeError: Network request failed"

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:21 39 4
gpt4 key购买 nike

在我的登录屏幕上的 react-native 应用程序中,我正在努力在输入错误的用户名/密码组合后为用户提供漂亮的错误消息。为了与 API 交互,我使用了库 Axios。但是,当我在 catch 语句中遇到错误时,我收到一条丑陋的错误消息,说我有一个“未处理的 promise 拒绝”,我无法执行设置组件状态或导航到新的组件等操作页面。

我看不出我做错了什么,它看起来和我在文档中看到的例子一模一样。

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

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: ",错误);:

"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)上的截图:

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

最佳答案

这段代码没有任何错误。

  • 您发送请求。
  • 您收到 401 - 未经授权的响应。
  • Catch 接收到错误并记录下来。

有趣的是为什么您会收到未处理的 promise 拒绝错误。但这被扔到别的地方了。所以你需要提供更多的代码。

编辑:也许您只需要将 axios promise 返回给调用函数?

关于javascript - Axios Promise 处理 - 在 react-native 中获取 "Possible Unhandled Promise Rejection - TypeError: Network request failed",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45991384/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com