Next.js Discord

Discord Forum

I got same error on my every API but it's works well in my local

Unanswered
Ibizan Hound posted this in #help-forum
Open in Discord
Ibizan HoundOP
give 500 in vercel deployment but works on my local, sharing more details.

3 Replies

Ibizan HoundOP
route.ts
import { res } from "@/lib/API";
import { connectToDatabase } from "@/db";
import {
  orderBy,
  limit,
  query,
  DocumentData,
  getDocs,
} from "firebase/firestore";

export async function POST(request: Request) {
  const { db, collection, addDoc, startedTime } = await connectToDatabase();

  const usersRef = collection("users");
  const q = query(usersRef, orderBy("claps", "desc"), limit(5));
  const querySnapshot = await getDocs(q);
  const trustpoints: DocumentData[] = [];
  querySnapshot.forEach((doc) => {
    const data = doc.data();
    trustpoints.push({
        name: data.name,
        claps: data.claps,
        image: data.image,
        isPremium: false
    });
  });

  return res({ message: "OK", base: "get trustpoints", data: trustpoints }, 200, startedTime);
}


@/lib/API func res
export function res(obj: any, status: number, startedTime: number) {
  const ex = new Error();
  const regex = /api\/(.*?)\/route/;
  const match = ex.stack?.match(regex);

  const now = performance.now();
  const seconds = ((now - startedTime) / 1000).toFixed(2);

  if (match) {
    const APIName = match[1];
    console.log(
      `+ Returning Value, API: \x1b[36m/api/${APIName}\x1b[0m, STATUS: \x1b[32m${status}\x1b[0m, Took: ${seconds}(s)${
        obj.message ? `, MESSAGE: ${obj.message}` : ""
      }`
    );
  } else {
    console.log("- IN RES FUNCTION, SOMETHINGS GONE WRONG.");
  }
  return NextResponse.json(obj, {
    status: status,
  });
}
error:
{
  "errorType": "Runtime.UnhandledPromiseRejection",
  "errorMessage": "TypeError: Failed to parse URL from /api/getTrustPoints",
  "reason": {
    "errorType": "TypeError",
    "errorMessage": "Failed to parse URL from /api/getTrustPoints",
    "stack": [
      "TypeError: Failed to parse URL from /api/getTrustPoints",
      "    at new Request (node:internal/deps/undici/undici:5272:19)",
      "    at i (/var/task/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:177224)",
      "    at F (/var/task/.next/server/chunks/638.js:1:39751)",
      "    at /var/task/.next/server/chunks/638.js:1:41900",
      "    at /var/task/.next/server/chunks/638.js:1:47601",
      "    at a.with (/var/task/.next/server/chunks/638.js:1:7547)",
      "    at s.with (/var/task/.next/server/chunks/638.js:1:1747)",
      "    at s.startActiveSpan (/var/task/.next/server/chunks/638.js:1:16802)",
      "    at a.startActiveSpan (/var/task/.next/server/chunks/638.js:1:17336)",
      "    at /var/task/.next/server/chunks/638.js:1:47453"
    ]
  },
  "promise": {},
  "stack": [
    "Runtime.UnhandledPromiseRejection: TypeError: Failed to parse URL from /api/getTrustPoints",
    "    at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)",
    "    at process.emit (node:events:529:35)",
    "    at emit (node:internal/process/promises:149:20)",
    "    at processPromiseRejections (node:internal/process/promises:283:27)",
    "    at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"
  ]
}
Ibizan HoundOP
and yeah API's working in Postman hell yeah