(edited)
Problem
I wanted to create a workflow but encountered an error at the submission of form.
Error: Only plain objects, and a few built-ins, can be passed to Server Actions. Classes or null prototypes are not supported.
How can I solve the error? I’m stuck in it now and need your help. Thanks in advance! The screenshot is as below:
Code
app/(dashboard)/workflows/_components/CreateWorkflowDialog.tsx:
'use client';import React, { useCallback, useState } from 'react';import {useForm} from 'react-hook-form';import {zodResolver} from '@hookform/resolvers/zod';import {useMutation} from '@tanstack/react-query';import {toast} from 'sonner';import {Layers2Icon, Loader2} from 'lucide-react';import { createWorkflowSchema, createWorkflowSchemaType } from '@/schema/workflow';import { CreateWorkflow } from '@/actions/workflows/createWorkflow';import { myLog } from '@/lib/utils';import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel } from '@/components/ui/form';import { Input } from '@/components/ui/input';import { Textarea } from '@/components/ui/textarea';import { Button } from '@/components/ui/button';function CreateWorkflowDialog({triggerText}: {triggerText?: string}) { const form = useForm<createWorkflowSchemaType>({ resolver: zodResolver(createWorkflowSchema), defaultValues: { name: '', // description: '', }, }); const {mutate, isPending} = useMutation({ mutationFn: CreateWorkflow, onSuccess: () => { toast.success('Workflow created', {id: 'create-workflow'}); },schema/workflow.ts:
import {z} from 'zod';export const createWorkflowSchema = z.object({ name: z.string().max(50), description: z.string().max(80).optional(),});export type createWorkflowSchemaType = z.infer<typeof createWorkflowSchema>;actions/workflows/createWorkflow.ts:
'use server'import { createWorkflowSchema, createWorkflowSchemaType } from '@/schema/workflow';export async function CreateWorkflow(form: createWorkflowSchemaType) { const {success, data} = createWorkflowSchema.safeParse(form); ......}Environment
next:14.2.5react:^18react-hook-form:^7.71.1zod:^4.3.5@tanstack/react-query:^5.90.20sonner:^2.0.7lucide-react:^0.562.0