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', }, } });
K
kapa.ai132d 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: ----------------------
K
kapa.ai132d ago
<@1142541928815153268> kapa.ai is still learning and improving, please let us know how it did by reacting below
X
xenial-black131d 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?
More Posts
getting 404 error when i click on create property buttonthese are my files import {useState} from 'react' import { FieldValues, useForm } from '@pankod/regetting 404 error when clicked on create button on refine client dashboard page add propertygetting 404 error when clicked on create button on refine client dashboard page add propertyHow to implement husky , prettier and eslint in refineHow to implement husky , prettier and eslint in refineStop the api from calling multiple timesconst { data: customerDetails, error } = useCustom< CustomerSearchResponse, ApiErrorResponsecall the api only on button click.const [searchTerm, setSearchTerm] = useState(''); // const [searchDetail, setSearchDetail] = usemantine project styling is brokenHI Guys I just started a refine project using remix, mantine and supabase. freshly install but the @refinedev/antd errorHello, we've recently ran `npm i` to install packages in our dev env but we are getting the followinMigrating to React Query v5Hello, is it safe to migration to React Query v5 using Refine? Our code is ok to go but Im not sure Error data fetchingHi Team. My questions is might be stupid but i'm very begineer in coding. so i get Error: Objects FineFood example is not workingI tried to open FineFood example from page: https://refine.dev/docs/examples/ but I got an error: Thnested naming not fetching data correactlyiam using useForm for my form and my input component looks like this <Autocomplete tRefine custom action resourceI have created a resource module like const leads = { name: Modules.LEADS, list: List, create: