Next.js Discord

Discord Forum

Failed to find Server Action "null".

Unanswered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
Receiving error in log Failed to find Server Action "null". This request might be from an older or newer deployment. Original error: Invariant: Missing 'next-action' header. Is anyone face this as well and can suggest any solution?

17 Replies

West African LionOP
@Silver Fox No luck yet
Horse guard wasp
@West African Lion @Siberian @Silver Fox I have the same error. I work with Mailchimp webhook. When Mailchimp sends notification to my Nextjs project this is the error i get on my server. any thoughts?
@Toyger are you using app or page router? is it api route or route handler, do you produce any response?
Horse guard wasp
Netxtjs.14, App router, API route, Mailchimp sends expected POST request ( URL-Encoded Data) but my application throws error when that reqest is sent
@Toyger app router have route handlers instead api routes https://nextjs.org/docs/app/building-your-application/routing/route-handlers are you using this?
Horse guard wasp
Sorry for confusion, Yes I use route handlers. Structure is src/app/api/webhook/route.ts
@Toyger can you show your code for route handler
Horse guard wasp
import { prisma } from "../../../../prisma/globalPrismaClientInstance";
import { NextApiRequest, NextApiResponse } from "next";



type MailchimpWebhookBody = {
  type: string;
  data: {
    email: string;
  };
};

export async function POST(
  req: NextApiRequest,
  res: NextApiResponse,
) {

  if (req.method === "POST") {
     const { type, data } = req.body as MailchimpWebhookBody;
    //console.log("Webhook type and data:", type, data); // 2

    if (type === "subscribe") {
      const email = data.email;

      try {
        const updateResult = await prisma.mailList.update({
          where: { email: email },
          data: { confirmed: true, subscriptionStatus: "subscribed" },
        });

        console.log(`Subscription confirmed for email: ${email}`, updateResult);

        res.status(200).json({ message: "Subscription confirmed" });
      } catch (error) {
       
        if (error instanceof Error) {
          res.status(500).json({
            error: "An error occurred while confirming the subscription.",
            details: error.message, // Be cautious with sending error details to the client for security reasons
          });
        } else {
    
          res.status(500).json({
            error: "An unexpected error occurred.",
          });
        }
      }
    } else if (type === "unsubscribe") {
      const email = data.email;
      try {
        const updateResult = await prisma.mailList.update({
          where: { email: email },
          data: { subscriptionStatus: "unsubscribed" },
        });
    
        res.status(200).json({ message: "Unsubscribe confirmed" });
      } catch (error) {
        res.status(500).json({
          error: "An error occurred while updating the unsubscribe status.",
        });
      }
    }
  }
}
I went through many trials and changes may be something is missing here now.
@Horse guard wasp import { prisma } from "../../../../prisma/globalPrismaClientInstance"; import { NextApiRequest, NextApiResponse } from "next"; type MailchimpWebhookBody = { type: string; data: { email: string; }; }; export async function POST( req: NextApiRequest, res: NextApiResponse, ) { if (req.method === "POST") { const { type, data } = req.body as MailchimpWebhookBody; //console.log("Webhook type and data:", type, data); // 2 if (type === "subscribe") { const email = data.email; try { const updateResult = await prisma.mailList.update({ where: { email: email }, data: { confirmed: true, subscriptionStatus: "subscribed" }, }); console.log(`Subscription confirmed for email: ${email}`, updateResult); res.status(200).json({ message: "Subscription confirmed" }); } catch (error) { if (error instanceof Error) { res.status(500).json({ error: "An error occurred while confirming the subscription.", details: error.message, // Be cautious with sending error details to the client for security reasons }); } else { res.status(500).json({ error: "An unexpected error occurred.", }); } } } else if (type === "unsubscribe") { const email = data.email; try { const updateResult = await prisma.mailList.update({ where: { email: email }, data: { subscriptionStatus: "unsubscribed" }, }); res.status(200).json({ message: "Unsubscribe confirmed" }); } catch (error) { res.status(500).json({ error: "An error occurred while updating the unsubscribe status.", }); } } } }
Toyger
I think that new router have only request as paramater, so you need to replace your response from
res.status(200).json({ message: "Subscription confirmed" });

to
 return new Response("Subscription confirmed", {
    status: 200,
  })


I don't remember how to return json with this type of Response, but for now you can probably return simple text/html output.
and you need to replace every occurence of it.
Also my issue is that i even dont see console.log when i call the webhook, to check if it was called at all
export async function POST(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  console.log("Webhook called")
  if (req.method === "POST") {
    const { type, data } = req.body as MailchimpWebhookBody;
    console.log("Webhook type and data:", type, data); // 2

    if (type === "subscribe") {
      const email = data.email;
in the very beginnig. This is full error
Error: Failed to find Server Action "null". This request might be from an older or newer deployment. Original error: Invariant: Missing 'next-action' header.
@Horse guard wasp Just tested. Same error.
Toyger
did you try to run it with postman/insomnia directly
@Toyger did you try to run it with postman/insomnia directly
Horse guard wasp
I tried postman. But something didnt work there. I will try again. Im new to postman