React Native 中可能出现未处理的承诺拒绝网络错误 [英] Possible Unhandled Promise Rejection Network Error in React Native
问题描述
我使用 express.js
设置我的本地服务器,它只是处理请求并返回简单的消息.
I setup my local server using express.js
, That simply handle request and return simple message.
app.get('/hello', (req, res) => {
res.send('Hello world !');
});
我执行了服务器并在网络浏览器上对其进行了测试,效果很好.
I executed server and test it on web-browser, it works well.
我只是想在我的 react-native
应用程序上执行此操作.
Simply I want to do that on my react-native
app.
这是我的action.js
文件
import axios from 'axios';
exports.helloButtonPressAct = (email, password) => {
return function (dispatch) {
return axios.get('http://localhost:3000/hello')
.then(function (response) {
console.log(response);
// and create action obj here
// dispatch(someAction(...))
})
.catch(function (error) {
throw error;
console.log(error);
});
};
};
它只返回 catch()
结果.
可能的未处理的承诺拒绝(id:0):网络错误错误:createError 时出现网络错误 ...
Possible Unhandled Promise Rejection (id: 0): Network Error Error: Network Error at createError ...
也许出了什么问题,但我找不到是什么.
Maybe something wrong but I couldn't find what is it.
我该如何解决这个问题?
How can I fix this?
推荐答案
已解决.
返回 axios.get('http://localhost:3000/hello')
到
返回 axios.get('http://10.0.0.2:3000/hello')
它有效.
在 Android 模拟器中,localhost
指的是它的设备.
In Android emulator, the localhost
refers its device.
https://github.com/facebook/react-native/issues/10404
这篇关于React Native 中可能出现未处理的承诺拒绝网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!