我有一个上传账户报表的钩子,它抛出了错误,但没有明确的错误
我最初清除了一些其他钩子并重写了 useStatement 钩子,但是,似乎我仍然遇到了一些错误。
我的实现如下,
export const useUploadStatement = () => {
const { refetch } = useGetStageOneTransactions();
return useMutation(uploadStatement, {
onSuccess: (data) => {
console.log("Upload successful:", data.message);
refetch();
},
onError: (error) => {
handleError(error, "Error uploading. Please check your network and retry!!!");
},
});
};
我以为问题是因为我将 uploadStatement 直接传递给 useMutation,我将其更改为如下所示,没有任何变化。
import { useMutation, useQuery } from "@tanstack/react-query";
import Utils from "../shared/localStorage";
import toast from "react-hot-toast";
import {
approveStageOne,
approveStageTwo,
getStageOneList,
getStageTwoList,
revertRecord,
uploadStatement
} from "../apis/reconciliation";
// Utility function to handle API errors
const handleError = (error, defaultMsg) => {
const message = error.response?.data?.msg || "An unexpected error occurred";
const errorMsg = error.response?.status === 500 ? defaultMsg : message;
toast.error(errorMsg, { position: "top-right" });
console.error(defaultMsg, error.response?.data?.error || message);
};
export const useUploadStatement = () => {
const { refetch } = useGetStageOneTransactions();
return useMutation((data) => uploadStatement(data), {
onSettled: () => {
refetch();
},
onError: (error) => {
handleError(error, "Error uploading. Please check your network and retry!!!");
},
onSuccess: (data) => {
console.log("Upload successful:", data.message);
},
});
};