Next.js Discord

Discord Forum

getting empty request object on vercel

Answered
European anchovy posted this in #help-forum
Open in Discord
Avatar
European anchovyOP
export async function GET(request: NextRequest) {
try {
const token = request.cookies.get("token")?.value;

if (!token) {
return NextResponse.json({
success: false,
msg: token is not available,
reqCookiesLog:request
});
}

const dataFromToken = await jwt.verify(token as string, "thisismytoken");

const user = await User.findOne({
email: (dataFromToken as JwtPayload).email,
});

if (!user) {
return NextResponse.json({ msg: "User not found OR invalid token" });
}

return NextResponse.json({
success: true,
msg: "get user successfully",
data: user,
});
} catch (error) {
console.log({ error });

return NextResponse.json({ success: false, msg: ERROR ${error} });
}
}
i have a query can anyone pls help
i have deployed my nextjs app on vercel, In GET api route i m accessing token in cookies which is there in headers inside network tab while calling api but the issue is i am getting empty request body
in local it is working properly
Here is my code
Answered by European anchovy
@DirtyCajunRice | AppDir @European sprat thank u for your response , fixed my issue by adding this export const dynamic = 'force-dynamic' in route file
View full answer

12 Replies

Avatar
DirtyCajunRice | AppDir
console log all the cookies.
Avatar
European anchovyOP
i am getting request as empty object, working fine on my local
Avatar
DirtyCajunRice | AppDir
if its an app dir route handler properly put into a route.js you literally cant get an empty object.
Avatar
European sprat
It shouldn't be cached (because you're using request) but maybe it's still be cached because of how you're trying to access the cookies rather than using cookies()

https://nextjs.org/docs/app/building-your-application/routing/route-handlers#opting-out-of-caching
Avatar
European anchovyOP
Error: Dynamic server usage: cookies while using NextJs

getting the above error on using this
const cookieStore = cookies()
const token = cookieStore.get('token')?.value
Avatar
DirtyCajunRice | AppDir
even cached the entire request object wouldnt be empty
Avatar
European anchovyOP
@DirtyCajunRice | AppDir @European sprat tried everything still getting empty request {} working fine in my local
i have this middleware too


import { NextResponse } from "next/server";
import { NextRequest } from "next/server";

// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {

  const token = request.cookies.get("token")?.value;
  console.log('middleware console ????? request.cookies', request.cookies);
  
  if (!token) {
    return NextResponse.redirect(new URL("/signup", request.url));
  }
  return null

}

// See "Matching Paths" below to learn more
export const config = {
  matcher: ["/"],
};
inside middleware i m getting token but in GET request i am getting empty
Avatar
DirtyCajunRice | AppDir
Make a minimum reproducible example on code sandbox
Avatar
European anchovyOP
Avatar
European anchovyOP
@DirtyCajunRice | AppDir @European sprat thank u for your response , fixed my issue by adding this export const dynamic = 'force-dynamic' in route file
Answer