0

I am just getting this error message: Failed to send email: data is not defined.

...What I am trying to do is to implement resend library into my project. I've made an email template, it is working as intended, form submission is working too, but when I come to this part... I am just getting this error.

/** @format */

import { NextResponse } from "next/server";
import { Resend } from "resend";
import EmailTemplateWelcome from "@/app/emails/EmailTemplateWelcome";

const resend = new Resend(process.env.RESEND_API_KEY);

export async function POST(request, response) {
  try {
    const body = await request.json();
    console.log("body", body);

    const {
      firstName,
      lastName,
    } = body;

    console.log("firstName: ", firstName);

    const data = await resend.emails.send({
      from: "onboarding@resend.dev",
      to: "my@mail.com",
      subject: "Getting The Error",
      react: EmailTemplateWelcome({
        firstName,
        lastName,
      }),
    });

    return NextResponse.json(data);
  } catch (error) {
    if (error instanceof Error) {
      console.log(`Failed to send email: ${error.message}`);
    }
    return NextResponse.json(
      {
        error: "Internal server error.",
      },
      {
        status: 500,
      }
    );
  }
}

  • In your `try` ... `console.log(data)` before your return .. Is it valid? – Zak Aug 14 '23 at 15:28
  • When I try to console.log(data) just before the return it doesn't print. – Milan Pjevic Aug 14 '23 at 15:45
  • Still, I don't know how to approach the problem. I double check for 20 times all what popped up in my head... Still, I can't see the solution. :S – Milan Pjevic Aug 14 '23 at 16:03
  • It means your `Resend(process.env.RESEND_API_KEY)` is returning empty or null .. You need to look into that function .. – Zak Aug 14 '23 at 16:31

0 Answers0