Javascript 在表单提交失败后响应重定向';我不能正常工作
我使用redux进行状态管理,使用formik进行表单管理。如果提交没有错误,我希望用户重定向到一个页面,但它不会等待请求完成,而是在提交后立即重定向到页面Javascript 在表单提交失败后响应重定向';我不能正常工作,javascript,reactjs,typescript,redux,formik,Javascript,Reactjs,Typescript,Redux,Formik,我使用redux进行状态管理,使用formik进行表单管理。如果提交没有错误,我希望用户重定向到一个页面,但它不会等待请求完成,而是在提交后立即重定向到页面 export const editService = ( serviceId: string, body: ServiceCreateType, serviceImage: File ) => async dispatch => { try { dispatch({ type: EDIT_SERVICE
export const editService = (
serviceId: string,
body: ServiceCreateType,
serviceImage: File
) => async dispatch => {
try {
dispatch({ type: EDIT_SERVICE });
const config = await axiosConfig('ownerAccessToken');
await axios.put(`${API_URL}/services/${serviceId}`, body, config);
if (serviceImage) {
await dispatch(uploadServiceImageForOwner(serviceId, serviceImage));
}
dispatch({
type: EDIT_SERVICE_SUCCESS,
payload: 'Service Edited successfully',
});
} catch (error) {
dispatch({ type: EDIT_SERVICE_FAILED, payload: getErrorMessage(error) });
}
};
如果servicererror
为空,我希望重定向用户
onSubmit={async values => {
await dispatch(
editService(
service.serviceId,
{
...values,
price: machinePrice(price),
durationMinutes: durationOnMinutes,
expertUserId: assignedExpertId,
},
serviceImage
)
);
if (!isLoading && !servicesError) {
router.push(
'/dashboard/expert/[expertname]?view=my-services&type=pending',
`/dashboard/expert/${userProfile?.expertProfile.name}?view=my-services&type=pending`
);
}
}}
像这样试试
export const editService = (
serviceId: string,
body: ServiceCreateType,
serviceImage: File) =>
dispatch => {
return new Promise((resolve, reject) => {
(async () => {
try {
dispatch({ type: EDIT_SERVICE });
const config = await axiosConfig("ownerAccessToken");
await axios.put(`${API_URL}/services/${serviceId}`, body, config);
if (serviceImage) {
await dispatch(uploadServiceImageForOwner(serviceId, serviceImage));
}
dispatch({
type: EDIT_SERVICE_SUCCESS,
payload: "Service Edited successfully"
});
resolve();
} catch (error) {
dispatch({
type: EDIT_SERVICE_FAILED,
payload: getErrorMessage(error)
});
resolve();
}
})();
});
};
在您的
editService
操作中,它是否返回承诺?使用editService
功能更新问题,我还添加了editService
功能@AmilaSenadheera承诺执行器函数不应是异步的。
应为1个参数,但得到0。您是否忘记在“Promise”的类型参数中包含“void”?
它会抛出这些errors@AnjulaSamarasinghe,更新了应答器现在重定向部分不工作您是否已检查!孤岛加载&!servicesError
这些是否正在放置日志?