API routes not working after deploying on vercel
Unanswered
i_lost_to_loba_kreygasm posted this in #help-forum
Can someone tell me why my api routes were working in local but not in vercel ?
45 Replies
hello?
Toyger
your api return 500 error check server logs
@Toyger your api return 500 error check server logs
but it works on my local though
@i_lost_to_loba_kreygasm but it works on my local though
Asian black bear
But it doesn't work on Vercel, so you need to check the server logs to find out why. Are you sure you put all of the environment variables you had in .env.local to Vercel?
@i_lost_to_loba_kreygasm here are my env vars on vercel 😦
Asian black bear
Great you are in the portal, what is the error message in the log?
You can search on the logs tab
import db from "../../../utils/mongo";
import { getSession } from 'next-auth/react';
export default async function handler(req, res) {
const session = await getSession({ req });
let foundEmail=await db.collection('users').findOne({email:session.user.email})
console.log(foundEmail);
res.status(200).json({posts:foundEmail.posts})
} this is my codeAsian black bear
It looks like whatever is happening in
getSession is not returning a user and then you are trying to read from session.user which is making the function stopThe error looks like something is trying to parse XML as JSON in the error at the top which is kind of vague but is often related to things like getting a 404 or access denied message
In either case, there is a very good chance that an environment variable is missing or wrong.
if env vars were wrong , wouldnt it also cause issue on local ?
I think your env variables should be different between local and vercel
if you're using google for oauth
then those keys are associated with a callback url
which is localhost when running locally, but your domain when in the cloud
typically you configure 3 sets of oauth keys, local, staging and prod
I changed my callback url too
Asian black bear
The env variables on your local machine are in
.env.local in your project folder. These are not commited to git or sent to Vercel.In Vercel the env variables come from that portal you showed, but you have to put them in yourself... it is not automatic
should I remove my env file from github repo ?
Southern rough shrimp
Just bake the secrets into the code
.env files are a scam
@i_lost_to_loba_kreygasm should I remove my env file from github repo ?
Yeah you should, unless the env file itself is encrypted, it shouldnt be included in your repo
Asian black bear
based on this chat, yes just remove them.
i removed them and its the same . i kinda want to share my screen and solve this 😦
Asian black bear
have you put the env values into the vercel portal,
@i_lost_to_loba_kreygasm here are my env vars on vercel 😦
i showed you though
Asian black bear
bro, it feels like you are not even doing the bare minimum to try
did you make sure everything from the file was there with correct values?
I copy pasted everything directly into the portal , there is no way , it messes it up
DAY 2 of seeking help
@Ray help ;-;
getsession does an extra fetch that's uneccary
@!=tgt yeah
signIn<P>(
provider?,
options?,
authorizationParams?): Promise< P extends RedirectableProviderType ? SignInResponse | undefined : undefined > can you turn this into js ? 😦Asiatic Lion
@i_lost_to_loba_kreygasm same problem here... have you solved your problem?
New Zealand
@i_lost_to_loba_kreygasm Same problem here. Did you find any solution?
Asiatic Lion
Maybe somes help here :
https://nextjs-faq.com/fetch-api-in-rsc
https://nextjs-faq.com/fetch-api-in-rsc
New Zealand
Ohkayy thank you!