【发布时间】:2022-01-15 11:04:33
【问题描述】:
我有一个登录功能,它应该触发 AXIOS POST 请求并获取响应并将其保存在 AsyncStorage 中(我在 React Native 中)。相同的代码适用于 React,但在这里我无法运行它。为了调试它,我放置了几个警报函数。登录功能并没有比“第二次调用”更进一步
我无法理解(现在好几天)为什么没有触发该功能的其余部分,如果是,则没有明显的错误。
这是我的功能:
export const login = (email, password) => async(dispatch) => {
try {
dispatch({ type: USER_LOGIN_REQUEST });
alert('first call')
const config = {
headers: {
"Content-type": "application/json"
}
}
alert('second call')
const { data } = await axios.post("url/login", {email, password}, config)
alert('third call')
dispatch({type: USER_LOGIN_SUCCESS, payload:data});
alert('fourth call')
alert(JSON.stringify(data) + `hello`)
await AsyncStorage.setItem("userInfo", JSON.stringify(data))
alert('final call')
alert(userInfo)
} catch (error) {
dispatch({
type: USER_LOGIN_FAIL,
payload:
error.response && error.response.data.message
? error.response.data.message
: error.message,
})
alert("This login attempt is unsuccessful");
alert(error)
}
}
【问题讨论】:
-
你为你的axios客户端配置了baseUrl吗?例如,尝试在
axios.post("url/login"...中调用完整的 url(带有 http://)您还可以添加try/catch以查看有关错误的更多详细信息:try { const { data } = await axios.post("url/login", {email, password}, config) } catch(e) { console.log('Something went wront, see?', e) }
标签: reactjs react-native axios