How to update a Sanity database using sanity-utils.ts?

14 Views Asked by At

Attempting to create a function to update my Sanity database using sanity-utils.ts. Unsure if this is the correct approach. Here's my code:

I want to provide a method for users to add data to the database after verification

export async function createBlog(): Promise<any> {
  const client = createClient({
    projectId: projectId,
    dataset: dataset,
    apiVersion: apiVersion,
  });

  const blogData: any = {
    title: "test blog",
    description: "zzz",
  };

  const createdBlog = await client.create(blogData).catch((error) => {
    throw new Error(`Error creating blog: ${error.message}`);
  });

  return createdBlog;
}

That code return me undefinied, I'm calling it in my getStaticProps

export const getStaticProps = async () => {
  try {
    const blogs: Blog[] = await getLatestBlogs(2);
    const tools: Tool[] = await getLatestTools();
    const createB: any = await createBlog();

    return { props: { blogs, tools, createB } };
  } catch (error) {
    console.error("Error fetching data:", error);
    return { props: { blogs: [], tools: [] } };
  }
};
0

There are 0 best solutions below