jolly-crimson
jolly-crimsonā€¢3y ago

making custom errorNotification in mutate function under useDelete hook

I'm getting this error on trying to delete an entry. Foreign key violation. update or delete on table "departments" violates foreign key constraint "sub_departments_department_id_fkey" on table "sub_departments": {"response":{"errors":[{"extensions":{"code":"constraint-violation","path":"$"},"message":"Foreign key violation. I need to change this error into my own custom error message. I'm using, const { mutate } = useDelete(); under the mutate function, onClick={ () => mutate({ id: record.id, resource: "departments", }) } Here I've to pass errorNotification property and modify the above default notification message into my custom message. @Omer
4 Replies
Omer
Omerā€¢3y ago
Hey @coops8 šŸ‘‹ , Thank you for contacting us! You can use errorNotification property of useDelete hook šŸ’Æ https://refine.dev/docs/api-reference/core/hooks/data/useDelete/#properties
const { mutate } = useDelete({
errorNotification: (error, values, resource) => ({
message: error?.message ?? error?.code ?? "Error!",
type: "error",
description: error?.reason;
})
successNotification: (data, values, resource) => ({
message: data?.message ?? "Success!",
type: "success",
description: data?.description;
}),
});
const { mutate } = useDelete({
errorNotification: (error, values, resource) => ({
message: error?.message ?? error?.code ?? "Error!",
type: "error",
description: error?.reason;
})
successNotification: (data, values, resource) => ({
message: data?.message ?? "Success!",
type: "success",
description: data?.description;
}),
});
jolly-crimson
jolly-crimsonOPā€¢3y ago
Thanks @Omer ... useDelete was expecting 0 arguments
Omer
Omerā€¢3y ago
Sorry my mistake. You can pass it to the "mutate" function
jolly-crimson
jolly-crimsonOPā€¢3y ago
so according to me instead of writing in the useDelete hook as an argument... we can pass it as an in the mutate ... like this šŸ‘‡ mutate({ id: record.id, resource: "departments", errorNotification: (error, values, resource) => ({ message: error ? "Department cannot be removed" : "Department removed", type: "error", description: "Prohibited!!", }), }) exactly šŸ™‚

Did you find this page helpful?