Gui*_*eRZ 9 react-native axios react-native-android expo
我有一个React-Native应用程序使用NodeJS后端,它提供API.
我的React-Native前端正在使用Expo和Axios来点击我的NodeJS API路径(使用Hapi,Joi,Knex),这将(例如)更新我的数据库(MySQL).
一切都适用于我的iOS模拟器.但是,在Android模拟器上,我的一些路由""的命中不起作用"",并出现以下错误消息:( Network Error - node_modules/axios/lib/core/createError.js:16:24 in createError
实际上,它有效,但前端没有检测到它......)
这很奇怪,因为就像我说的那样,这只适用于我的一些路线.我更改了http://localhost:8000/api
以http://10.0.2.2:8000/api
确保Android模拟器访问我的API,这部分也没问题.
有缺陷的路线在iOS上正常工作,并且在Insomnia/Postman(localhost:8000/api/sendMail
)上正常工作.它正在Android Emulator上工作,但应用程序没有检测到它.
这是我的代码示例:
FRONT - 按下我的按钮"SendEmail":
/* Button.js */
const sendAndEmailPromise = await sendEmail(this.state.email);
console.log(sendAndEmailPromise); // Output : NOTHING (not undefined, not null, N O T H I N G).
if (sendAndEmailPromise.status === 200) {
// handle it
} else (sendAndEmailPromise.status === 403) {
// etc. for each of my codeStatus that can be returned by my API
}
/* MyAPI.js */
export function sendEmail(mail) {
return axiosInstance
.post(`/sendEmail`, null, {
params: {
mail
},
})
.then(response => response) // Will not enter here
.catch(error => {
console.log(error); // Will output : NETWORK ERROR
});
}
Run Code Online (Sandbox Code Playgroud)
BACK - 这是sendEmail的承诺:
// Will return true of false :
const isMailSend = await sendTheEmail(mail);
console.log(isMailSend); // Output : TRUE and I receive the email on my gmail, so Android Emulator HIT the route.
if (isMailSend) {
return handler
.response({
data: {
message: "Email sent.",
},
})
.code(200);
} else {
// Handle it and return another response
}
Run Code Online (Sandbox Code Playgroud)
我希望我的前面现在一切正常(实际上发生了......)并得到代码状态,而不是"网络错误".
更多,Axios是否有可能获得另一个级别的错误?更具体的东西.
GitHub问题并不完全相同,但似乎与某些东西有关:https://github.com/axios/axios/issues/973
谢谢.
你在这里处理两个问题:
1)您的模拟器无法正常工作,因为它无法将单词"localhost"解析为ip.就像使用localhost2.com一样,它无法解决问题.这必须指向您的服务器192.xxx的本地IP
2)您的服务器必须绑定到0.0.0.0,因为通过绑定到localhost它无法解析为本地请求,如192.xxx如果您修复绑定,您的模拟器将有可能看到服务器和邮递员也将.
归档时间: |
|
查看次数: |
4231 次 |
最近记录: |