I’m trying to submit a form using Next.js, Resend, React Email. The browser gives ‘500 Internal Server Error’ and the reason is that ‘TypeError: req.json is not a function’. How to solve it? Is there any ideas?
async function onSubmit(values: FormInput) {
await fetch('/api/send/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
fullname:values.fullname,
email:values.email,
phone:values.phone,
message:values.message,
}),
}).then(() => {
console.log('Application Submitted');
});
reset();
}
And in send.ts file I write this code
import { Resend } from 'resend';
import { NextRequest, NextResponse } from 'next/server';
import EmailCompany from '@/components/job-container/EmailCompany';
const resend = new Resend('re_Pu73K6n2_CktZzeybNrLrf6EA65sqHXmD');
async function POST(req: NextRequest) {
const { name, email, phone, message, file, attachments } = await req.json();
try {
const data = await resend.emails.send({
from: '[email protected]',
to: email,
subject: `Application for Open Positions`,
react: EmailCompany({ name, email, phone, message, file }),
});
return NextResponse.json(data);
} catch (error) {
return NextResponse.json({ error });
}
}
export default POST;
When I am trying print ‘console.log({ req: req.body })’, it returns me empty object.Why does this happen and how to fix it?
New contributor
Elmira Grigoryan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.