JamalJ
Convex Community3y ago
16 replies
Jamal

NextJs Server Actions + Convex not working

Im trying to use convex http client in nextjs server actions to submit data. However its not working. When i console log the form data, i get all my valid data. However the mutation does not submit to convex? It does not run at all and theres no errors i can see.
'use server';

import { convexClient } from '@/lib/convex';
import { api } from '../../convex/_generated/api';

export async function subscribeToWaitingList(formData: FormData) {
    try {
        let email = formData.get('email') as string | undefined;

        if (!email) throw new Error('Email is required');

        const check = await convexClient.query(api.waiting_list.alreadySubscribed, {
            email,
        })

        if(check === "subscribed") {
            return {
                success: false,
                error: 'You are already subscribed to the waiting list',
            }
        }

        await convexClient.mutation(api.waiting_list.create, {
            email,
        });

        return {
            success: true,
            error: null,
        }
    } catch (error: any) {
        return {
            success: false,
            error: error.message,
        };
    }
}
Was this page helpful?