Next.js Discord

Discord Forum

onSubmit not working

Answered
Yellowstripe scad posted this in #help-forum
Open in Discord
Yellowstripe scadOP
const onSubmit = () => {
        useEffect(() => {
            Vote(groupSelected, foundSwitch.id)
            .then((voteStatus) => {
                if (voteStatus == true) {
                    return (
                        router.push("/vote/thanks")
                    )
                }
            })
            .catch((error) => {
                toast.error(error.message);
            });
        }, []);
    }


"use server"

import { db } from "@/lib/db";
import { getServerSession } from "next-auth/next"
import { authOptions } from "@/lib/auth";

export default async function Vote(vote: string[], switchValue: string) {
    console.log("here 0")
    try {
        const { username } = (await getServerSession(authOptions))?.user as any;
        console.log("here1")
        await db.user.update({
            where: { username },
            data: { votedFor: { push: switchValue } },
        });
        console.log("here2")
        const updatedVote = vote.map((value: string) => value.replace(/-/g, "_"));
        console.log("here3")
        for (const updatedValue of updatedVote) {
            await db.preVotingSwitches.update({
                where: { id: switchValue },
                data: { [updatedValue]: { increment: 1 } },
            });
        }
        console.log("here4")
        return true
    } catch (error) {
        console.log(error);
        console.log("here5")
        return false
    }
}


nothing is getting logged into my console
Answered by Ray
<Button onClick={onSubmit} ... />
View full answer

6 Replies

why are you using useEffect inside onSubmit?
@Ray why are you using `useEffect` inside `onSubmit`?
Yellowstripe scadOP
I thought that's how youre supposed to do it, I removed it and it still doesnt work
where do you use the onSubmit
@Ray where do you use the onSubmit
Yellowstripe scadOP
in the button right below
<Button onClick={onSubmit} ... />
Answer