Can anyone tell me why I am getting this error:
Here is my Sign Up front end code
`"use client";
import { zodResolver } from "@hookform/resolvers/zod";
import React from "react";
import { useForm, type FieldValues } from "react-hook-form";
import { signUpSchema, TSignUpSchema } from "@/lib/types";
import Link from "next/link";
import { useRouter } from "next/navigation";
function SignUp() {
const router = useRouter();
const {
register,
handleSubmit,
formState: { errors, isSubmitting, isLoading },
reset,
setError,
} = useForm\<TSignUpSchema\>({
resolver: zodResolver(signUpSchema),
});
const onSubmit = async (data: TSignUpSchema) =\> {
try {
const response = await fetch("/api/signup", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (response.status === 400) {
setError("email", {
type: "duplicate email",
message: "email already exists",
});
}
if (response.status === 200) {
router.push("/login");
}
} catch (err) {
setError("root", {
type: "unknown",
message: "unknown error!!please try again",
});
}
console.log(data);`
Here is my Servcer Side Code
import { signUpSchema } from "@/lib/types";
import { NextResponse } from "next/server";
import UserModel from "@/models/User";
import connect from "@/utils/db";
import bcrypt from "bcryptjs";
export async function POST(request: Request) {
const body = await request.json();
const { email_raw, password_raw } = body;
const email = signUpSchema.safeParse(email_raw);
const password = signUpSchema.safeParse(password_raw);
await connect();
const existingUser = await UserModel.findOne({ email });
if (existingUser) {
return new NextResponse("Email is already in use", { status: 400 });
}
if (typeof password !== "string") {
return new NextResponse("Password must be a string", { status: 400 });
}
const hashedPassword = await bcrypt.hash(password, 5);
const newUser = new UserModel({
email,
password: hashedPassword,
});
try {
await newUser.save();
return new NextResponse("user is registered", { status: 200 });
} catch (err: any) {
return new NextResponse(err.message || "Internal Server Error", {
status: 500,
});
}
}
Please help on this error. I expected to reach the login Page. Have got no clue what I am doing wrong. But getting the following error:"http://localhost:3000/api/signup 500 (Internal Server Error)". Kind of new here. Would really appreciate the help.