not throwing error on duplicates, but also not creating zoderror
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m22s

This commit is contained in:
ChrQR 2024-06-02 12:43:10 +02:00
parent a065e6b592
commit aa209bc0a8

View File

@ -4,6 +4,7 @@ import { revalidatePath } from "next/cache";
import { db } from "../db";
import { countries } from "../db/schema";
import { ZodError, z } from "zod";
import { eq } from "drizzle-orm";
const schema = z.object({
name: z.string().min(1, { message: "Name is required" }),
@ -11,10 +12,21 @@ const schema = z.object({
export async function addCountry(prevstate: any, formData: FormData) {
const name = formData.get("name") as string;
const exists = await db
.select({ name: countries.name })
.from(countries)
.where(eq(countries.name, name));
try {
schema.parse({
name,
});
if (!exists[0]) {
await db
.insert(countries)
.values({ name })
.returning({ name: countries.name });
revalidatePath("/");
}
return {
message: "success",
errors: undefined,