我有一个 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
to http://10.0.2.2:8000/api
确保 Android Emulator 访问我的 API,这部分没问题。
buggy路线在iOS上工作正常,在Insomnia / Postman上工作正常(localhost:8000/api/sendMail
)。它正在 Android 模拟器上运行,但应用程序未检测到它。
这是我的代码的示例:
正面——按下我的按钮“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
});
}
返回 - 这是 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
}
我希望我的前端现在一切正常(实际上发生了......)并获得其代码状态,而不是“网络错误”。
此外,Axios 是否有可能出现另一个级别的错误?更具体的事情。
一个 GitHub 问题,虽然不完全相同,但似乎与等效内容相关:https://github.com/axios/axios/issues/973 https://github.com/axios/axios/issues/973
谢谢。