Next.js Discord

Discord Forum

getting a random error

Answered
Saltwater Crocodile posted this in #help-forum
Open in Discord
Avatar
Saltwater CrocodileOP
import { NextResponse } from "next/server"; import sgMail from "@sendgrid/mail"; import dotenv from "dotenv"; dotenv.config(); sgMail.setApiKey(process.env.SENDGRID_API_KEY); export async function POST(request) { const email = await request.json(); const message = { to: email, from: "mykolas.hehe@gmail.com", subject: "receipt", text: "text1", html: "<h1>text1</h1>", }; // return NextResponse.json({data: email}) try { await sgMail.send(message).then(() => { return NextResponse.json({ succes: "pavyko issiusti emaila" }); }); } catch (error) { return NextResponse.json({ error: error }); } }

and i get this error in my terminal:

error TypeError: Cannot read properties of undefined (reading 'headers') at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:277:61) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

this error in my catch:

AxiosError {message: 'Request failed with status code 500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', config: {…}, request: XMLHttpRequest, …}

i doubt that it is front end error because there is commented out code which passes back the email and it works
Answered by joulev
try this
try {
    await sgMail.send(message);
    return NextResponse.json({ succes: "pavyko issiusti emaila" });
  } catch ...
View full answer

2 Replies

Avatar
try this
try {
    await sgMail.send(message);
    return NextResponse.json({ succes: "pavyko issiusti emaila" });
  } catch ...
Answer
Avatar
thanks for the kudo, i suppose that means this issue is resolved right