getting empty request object on vercel
Answered
European anchovy posted this in #help-forum
European anchovyOP
export async function GET(request: NextRequest) {
try {
const token = request.cookies.get("token")?.value;
if (!token) {
return NextResponse.json({
success: false,
msg:
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:
}
}
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
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
12 Replies
console log all the cookies.
@DirtyCajunRice | AppDir console log all the cookies.
European anchovyOP
i am getting request as empty object, working fine on my local
if its an app dir route handler properly put into a route.js you literally cant get an empty object.
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
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#opting-out-of-caching
@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
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
getting the above error on using this
const cookieStore = cookies()
const token = cookieStore.get('token')?.value
@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
even cached the entire request object wouldnt be empty
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
@European anchovy <@184479429404262410> <@135324139648057344> tried everything still getting empty request {} working fine in my local
Make a minimum reproducible example on code sandbox
European anchovyOP
@DirtyCajunRice | AppDir https://codesandbox.io/p/sandbox/upbeat-driscoll-4mvj2z?file=/app/page.tsx:15,20
is this valid
is this valid
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