#Error when setting up client action

2 messages · Page 1 of 1 (latest)

crystal karma
#

Error:

No overload matches this call.
  Overload 1 of 2, '(action: (state: { message: string; }) => { message: string; } | Promise<{ message: string; }>, initialState: { message: string; }, permalink?: string | undefined): [state: { message: string; }, dispatch: () => void]', gave the following error.
    Argument of type '(prevState: string, formData: FormData) => Promise<{ message: string; }>' is not assignable to parameter of type '(state: { message: string; }) => { message: string; } | Promise<{ message: string; }>'.
      Target signature provides too few arguments. Expected 2 or more, but got 1.
  Overload 2 of 2, '(action: (state: { message: string; }, payload: FormData) => { message: string; } | Promise<{ message: string; }>, initialState: { message: string; }, permalink?: string | undefined): [state: ...]', gave the following error.
    Argument of type '(prevState: string, formData: FormData) => Promise<{ message: string; }>' is not assignable to parameter of type '(state: { message: string; }, payload: FormData) => { message: string; } | Promise<{ message: string; }>'.
      Types of parameters 'prevState' and 'state' are incompatible.
        Type '{ message: string; }' is not assignable to type 'string'.ts(2769)
(alias) function createBooking(prevState: string, formData: FormData): Promise<{
    message: string;
}>
import createBooking

the action code:

'use server'

import { createDocument, deleteDocument, findDocument, updateDocument } from "./utils/db0perations";

import { revalidatePath } from "next/cache";
import { useFormState } from "react-dom";
import { z } from "zod";

export async function createBooking(prevState:string, formData: FormData){
    const schema = z.object({
        name: z.string().min(1),
        email: z.string().email().min(5),
        message: z.string().min(1),
    })
    const data = schema.parse({
        name: formData.get('name'),
        email: formData.get('email'),
        message: formData.get('message'),
    });


    try {
        createDocument(data, 'bookings');
        return {message: 'Booking created successfully'};
    } catch (error) {
        console.error(error);
        return {message: 'An error occurred while creating the booking'};
    }
}
merry sirenBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)