Next.js Discord

Discord Forum

API route not found in .next directory

Answered
readme posted this in #help-forum
Open in Discord
Avatar
In my Next.js application one of my api routes is not found in the .next directory after executing the command npm run build. As you can see the given images the /api/receipts/update/[id] route is not found the .next folder due to which i am having issues fetching this api route. Any suggestions??
Image
Image
Answered by readme
nvm i found the error
i named the route..js instead of route.js. Thank you for you help @gin
View full answer

48 Replies

Avatar
these are the logs. Sorry for the small font in the image
Image
Avatar
does it work on dev?
Avatar
i have not deployed this application yet. This does not work on my local environment. I tried deleting the .next folder as well but upon rebuilding it i get the same problem.
Avatar
deployed?
i asked if it works on dev
the route
Avatar
Nope it does not
Avatar
what errors do u get
Avatar
I get a 405 method not found error
I’ll show you the screenshots
Avatar
screenshot route.js
Avatar
Image
The one in the very bottom
I’m using kinde auth for this application
Avatar
but thats a completely different route tho
Avatar
Ok
I changed the name from edit to update
But the problem remains the same
Avatar
u was talking about /api/receipts/update/[id]
Avatar
I’ll send route.js
Yes I changed the directory name from edit to update
But it’s the same route
Avatar
whats inside [id] folder
Avatar
after the middleware i directly have my route.js

import { db } from "@/config/firebase";
import { getReceipt } from "@/helpers/getReceipt";
import { NextResponse } from "next/server";

export const PUT = async (req, { params }) => {
  try {
    console.log(req)
    const data = await req.json();
    const receiptId = params.id;
    const receiptRef = db.collection("receipts").doc(receiptId);
    await receiptRef.update(data);

    const updatedReceipt = await getReceipt(receiptId)

    return NextResponse.json(
      {
        message: "Receipt updated successfully",
        receipt: updatedReceipt,
      },
      { status: 200 }
    );
  } catch (error) {
    console.log(error);
    return NextResponse.json(
      {
        error: "Error updating image",
      },
      { status: 400 }
    );
  }
};
    
Avatar
if there is a route.js screenshot the code
Avatar
Ok hold on
Let me start my pc
Avatar
what route is this?
try
export async function PUT(
  request: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  const slug = (await params).id // 'a', 'b', or 'c'
}
Avatar
/api/receipts/update/[id]
Avatar
remove the typescript part
Avatar
Ok
Avatar
export async function PUT(request, { params }) {
  const slug = (await params).id // 'a', 'b', or 'c'
}
Avatar
i could do it but the issue is that i redirects to the /api/auth/login page anyways so im not gonna enter this route
this is the error log
Image
Avatar
why does that redirect happen?
do u have something in middleware?
Avatar
the redirect happens because kinde auth thinks that the token that i am providing is not valid hence i get the getUser error. But all my other routes are made in the exact same fashion and have not errors whatsoever. The token and headers are all being logged correctly in the middleware so that's not an issue
Avatar
maybe the PUT request is not accepted by your auth
Avatar
The issue is that since the .next folder does not recognize the /api/receipts/update/[id] the request does not hit that route.
I've tried POST as well i get the same result.
Avatar
hmm
Avatar
so that's how i knew that i'ts a next js issue not a kinde auth issue
so i'm trying to find a way to get the /api/receipts/update/[id] folder in the .next folder
Avatar
nvm i found the error
i named the route..js instead of route.js. Thank you for you help @gin
Answer
Avatar
it works now
Avatar
no proble
glad it works