可能未处理的承诺拒绝(id:0):Redux 中的 axios
Possible Unhandled Promise Rejection (id: 0): axios in Redux
我正在调用 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);
}
};
我正在调用 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);
}
};