可能未处理的承诺拒绝(id:0):Redux中的axios



我正在调用API,但它会给我一个警告:Possible Unhandled Promise Rejection (id: 0):这是我的代码:actions.js

import {BASE_URL} from '../Http/config';
import * as Action from './actionTypes';
export const userLoggedIn = ({emphone, pass}) => {
const formdata = new FormData();
formdata.append('emphone', emphone);
formdata.append('pass', pass);
formdata.append('submit', 'submit');
try {
return async dispatch => {
const res = await axios({
method: 'POST',
url: BASE_URL + '/login.php',
data: formdata, //'emphone','pass','submit
// body: formdata,
});
if (res.data) {
dispatch({
type: Action.USER_LOGGED_IN,
payload: {
userPhone: formdata.get('emphone'),
userToken: res.data.response.token,
},
});
console.log(res.data);
}
};
} catch (error) {
console.log(error);
}
};

在LoginScreen提交处理程序中调用操作:

dispatch(userLoggedIn({emphone: '8107224909', pass: 'shub@123'}));

我在互联网上搜索了这个警告,但我找到了一些解决方案,但它们似乎让我的实现感到困惑。任何帮助都将不胜感激。

您需要在调度函数中移动try/catch

return async dispatch => {
try {
const res = await axios({
method: "POST",
url: BASE_URL + "/login.php",
data: formdata, //'emphone','pass','submit
// body: formdata,
});
if (res.data) {
dispatch({
type: Action.USER_LOGGED_IN,
payload: {
userPhone: formdata.get("emphone"),
userToken: res.data.response.token,
},
});
console.log(res.data);
}
} catch (error) {
console.log(error);
}
};

相关内容

  • 没有找到相关文章

最新更新