I have been trying to post a new user to my sanity content lake using the sanity mutation but for almost a week now, I kept on getting same error. This is the error:
http://localhost:3000/api/register net::ERR_ABORTED 500 (Internal Server Error)
and
Uncaught (in promise) Error: Failed to submit form
at handleSubmit
This this the submit function:
const Register = () => {
const [firstName, setFirstName] = useState("");
const [surname, setSurname] = useState("");
const [email, setEmail] = useState("");
const [phone, setPhone] = useState("");
const [password, setPassword] = useState("");
const handleSubmit = async (e: any) => {
e.preventDefault();
const response = await fetch("/api/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ firstName, surname, email, phone, password }),
});
if (!response.ok) {
throw new Error("Failed to submit form");
}
};
and this is the API route:
export async function POST(request: Response) {
const { firstName, surname, email, phone, password } = await request.json();
await fetch(`https://${projectId}.api.sanity.io/v2021-10-21/data/mutate/${dataset}`, {
method: "post",
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${sanity_API_Token}`,
},
body: JSON.stringify({ _type: "user", firstName, surname, email, phone, password }),
});
return NextResponse.json({ message: "Added successful" });
}