broad-brown
broad-brown8mo ago

Get inserted ID after useCreate runs

Refine + Supabase After I run useCreate(), the record inserts, but I am getting no data in the response. I would like to get the id of the record just created. After a pile of research I discovered if I add the config below to my supabaseClient, the response is as expected with the results of the insert returned in the data object. I feel as if I am missing something here. I am jumping in the deep end leaning lots of new frameworks all at the same time; so I might be doing something very non-standard or thinking something wrong. My take on this is that there must be a significantly easer way to get the id after useCreate is run. export const supabaseClient = createClient(SUPABASE_URL, SUPABASE_KEY, { db: { schema: "public", }, auth: { persistSession: true, }, global: { headers: { Prefer: 'return=representation', }, } });
3 Replies
kapa.ai
kapa.ai8mo ago
Hi @dervishmoose_46979. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- In Refine, the useCreate hook is used to create new records. When the mutation is successful, it returns a data object which should contain the auto-generated ID of the newly created record. This data object is one of the parameters received by the onSuccess or onMutationSuccess callback function. Here is a basic usage example of useCreate:
import { useCreate } from "@refinedev/core";

const { mutate } = useCreate();

mutate({
resource: "products",
values: {
name: "New Product",
material: "Wood",
},
});
import { useCreate } from "@refinedev/core";

const { mutate } = useCreate();

mutate({
resource: "products",
values: {
name: "New Product",
material: "Wood",
},
});
If you're getting undefined for data, it might be due to the response from your dataProvider. Ensure that your dataProvider's create method is returning the correct data. In your case, you are using Supabase as your data provider. You have correctly set the Prefer header to return=representation in your Supabase client configuration. This instructs Supabase to return the inserted record in the response. If you are still not getting the data in the response, you might want to check your dataProvider's create method and ensure it is correctly handling the response from Supabase. Here's an example of how you can use the mutate function and get the auto-generated ID:
import { useCreate } from "@refinedev/core";

const { mutate } = useCreate();

const createNewRecord = async () => {
const { data } = await mutate({
resource: "products",
values: {
name: "New Product",
material: "Wood",
},
});

console.log(data.id); // This should log the auto-generated ID
};

createNewRecord();
import { useCreate } from "@refinedev/core";

const { mutate } = useCreate();

const createNewRecord = async () => {
const { data } = await mutate({
resource: "products",
values: {
name: "New Product",
material: "Wood",
},
});

console.log(data.id); // This should log the auto-generated ID
};

createNewRecord();
In this example, data is the result of the mutation and data.id should be the auto-generated ID of the newly created record. For more information, you can refer to the Refine documentation (https://refine.dev/docs/core/hooks/data/use-create/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai8mo ago
<@1142541928815153268> kapa.ai is still learning and improving, please let us know how it did by reacting below
broad-brown
broad-brown8mo ago
Yeah, that is the answer if Supabase is returning anything in the data object. In my case it was returning a blank object. I had to add the Prefer: 'return=representation' header value to get it to work as described. Am I doing something wrong or have a bad confing?