Next 15 function gives 502 error when deployed to Netlify

Hello, Netlify support.

I successfully deployed my app on Netlify but I keep getting an error Request failed with status code 502 when I try to make an API request to a function. I am using next: 15.3.3. The app works fine locally.

Here’s the function:

import { headers } from "next/headers";
import { NextResponse, NextRequest } from "next/server";
import { verifyJWT } from "../../_actions/auth/jwt-verification";
import userModel from "@/models/userModel";

export const GET = async (_req: NextRequest) => {
  try {
    const requestHeaders = await headers();
    const authorization_headers = requestHeaders.get("token");

    if (!authorization_headers) {
      return NextResponse.json({
        success: false,
        message: "Not Authorized. Please Login.",
      });
    }

    const user_id = await verifyJWT(authorization_headers);

    let user = await userModel.findById(user_id).lean();

    if (!user) {
      return NextResponse.json({ success: false, message: "User not found" });
    }

    if (!user.cartData) {
      user.cartData = {};
    }

    return NextResponse.json({ success: true, user });
  } catch (error) {
    console.log(error);
    return NextResponse.json({ success: false, message: error.message });
  }
};

My initial assumption was that netlify doesn’t support next/headers but from this post, I ruled that option out. Any kind of help resolving this will be highly appreciated.

@P-crypto Have you checked the Function logs?

Yes. On my console, I keep getting a generic error: {"errorType":"Error","errorMessage":"An unknown error has occurred"}

On Netlify’s function logs page:

Jun 19, 04:19:44 PM: 3e915f6f Duration: 2514.28 ms Memory Usage: 192 MB
Jun 19, 04:19:51 PM: 48640182 Duration: 10000 ms Memory Usage: 197 MB
Jun 19, 04:21:06 PM: 90c9631b INFO   DB Connected!!
Jun 19, 04:21:06 PM: 90c9631b INFO   Mongoose DB name: eridanus-mall
Jun 19, 04:21:06 PM: 90c9631b INFO   {}
Jun 19, 04:21:07 PM: 90c9631b Duration: 410.73 ms Memory Usage: 202 MB
Jun 19, 04:21:09 PM: 043c868c INFO   [FCM] Firebase initialized
Jun 19, 04:21:09 PM: 043c868c INFO   [FCM] Skipped messaging init — running on server
Jun 19, 04:21:17 PM: 043c868c Duration: 10000 ms Memory Usage: 202 MB

which function? Do you mind sharing steps to reproduce?

I just found the fix; I was failing to connect to the DB before performing the query. Thanks for trying to help.