Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在表单提交失败后响应重定向';我不能正常工作_Javascript_Reactjs_Typescript_Redux_Formik - Fatal编程技术网

Javascript 在表单提交失败后响应重定向';我不能正常工作

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

我使用redux进行状态管理,使用formik进行表单管理。如果提交没有错误,我希望用户重定向到一个页面,但它不会等待请求完成,而是在提交后立即重定向到页面

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
这些是否正在放置日志?