flat-fuchsia
flat-fuchsia2mo ago

Get response from onCreate mutate

I want to create a resource item in my backend, so I thought I would use useCreate instead of a regular post request, however I want to now be able to read the response of this request.
const { mutate } = useCreate();


mutate({
resource: "tenants",
values: {
name: form.getFieldValue("name"),
priceId: form.getFieldValue("priceId")
}
})
const { mutate } = useCreate();


mutate({
resource: "tenants",
values: {
name: form.getFieldValue("name"),
priceId: form.getFieldValue("priceId")
}
})
9 Replies
kapa.ai
kapa.ai2mo ago
Hi @BlackBearFTW. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The mutate function from useCreate returns a promise that resolves with the result of the mutation. You can use this to read the response of the request. Here's how you can do it:
const { mutate } = useCreate();

mutate({
resource: "tenants",
values: {
name: form.getFieldValue("name"),
priceId: form.getFieldValue("priceId")
}
}).then((response) => {
console.log(response);
}).catch((error) => {
console.error(error);
});
const { mutate } = useCreate();

mutate({
resource: "tenants",
values: {
name: form.getFieldValue("name"),
priceId: form.getFieldValue("priceId")
}
}).then((response) => {
console.log(response);
}).catch((error) => {
console.error(error);
});
In the code above, response will contain the result of the mutation. If the mutation is successful, response will contain the data returned by the server. If the mutation fails, the promise will be rejected and the catch block will be executed. For more information, you can refer to the useCreate documentation (https://refine.dev/docs/data/hooks/use-create/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@490961337896402944> kapa.ai is still learning and improving, please let us know how it did by reacting below
flat-fuchsia
flat-fuchsia2mo ago
@kapa.ai that doesn't work
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you. One moment! :books:
flat-fuchsia
flat-fuchsia2mo ago
const { mutate } = useCreate();

const onFinish = async (values: any) => {

const response = await mutate({
resource: "tenants",
values: {
name: form.getFieldValue("name"),
priceId: form.getFieldValue("priceId")
}
})

console.log(response)
};
const { mutate } = useCreate();

const onFinish = async (values: any) => {

const response = await mutate({
resource: "tenants",
values: {
name: form.getFieldValue("name"),
priceId: form.getFieldValue("priceId")
}
})

console.log(response)
};
kapa.ai
kapa.ai2mo ago
---------------------- I'm sorry to hear that the solution didn't work for you. Unfortunately, based on the information provided in the knowledge sources, I can't provide a more specific solution. The useCreate hook from Refine should return a mutate function that can be used to perform the create operation and it should return a Promise that resolves with the server's response. If this isn't working as expected, it might be due to a variety of factors such as network issues, server configuration, or a bug in Refine. I would recommend checking the network tab in your browser's developer tools to see if the request is being sent and what response is being returned. If the request is being sent and a response is being returned, then the issue might be with how you're trying to read the response. If you're still having issues, you might want to consider reaching out to the Refine community on Discord (https://discord.com/invite/refine) or opening an issue on the Refine GitHub repository (https://github.com/pankod/refine/issues). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@490961337896402944> kapa.ai is still learning and improving, please let us know how it did by reacting below
wise-white
wise-white2mo ago
Hello @BlackBearFTW , you can use onSuccess
const { mutate } = useCreate();

mutate(
{
resource: "products",
values: {
name: "New Product",
material: "Wood",
},
},
{
onError: (error, variables, context) => {
// An error occurred!
},
onSuccess: (data, variables, context) => {
// Let's celebrate!
},
},
);
const { mutate } = useCreate();

mutate(
{
resource: "products",
values: {
name: "New Product",
material: "Wood",
},
},
{
onError: (error, variables, context) => {
// An error occurred!
},
onSuccess: (data, variables, context) => {
// Let's celebrate!
},
},
);
flat-fuchsia
flat-fuchsia2mo ago
Thank you