#API routes not working after deploying on vercel
47 messages ยท Page 1 of 1 (latest)
๐ This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
๐ต๏ธ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
โ
You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
hello?
your api return 500 error check server logs
but it works on my local though
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?
here are my env vars on vercel ๐ฆ
Great you are in the portal, what is the error message in the log?
You can search on the logs tab
this is one api route
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 code
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 stop
The 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
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 ?
Yeah you should, unless the env file itself is encrypted, it shouldnt be included in your repo
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 ๐ฆ
have you put the env values into the vercel portal,
i showed you though
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
use getserversession instead btw
getsession does an extra fetch that's uneccary
Do not ping other people in order to get attention to your question unless they are actively involved in the discussion. If you're looking to get help, it is a lot better to post your question in a public channel so other people can help or learn from the questions
is the redirect url right ?
signIn<P>(
provider?,
options?,
authorizationParams?): Promise< P extends RedirectableProviderType ? SignInResponse | undefined : undefined >``` can you turn this into js ? ๐ฆ
@latent zephyr same problem here... have you solved your problem?
@latent zephyr Same problem here. Did you find any solution?
Maybe somes help here :
https://nextjs-faq.com/fetch-api-in-rsc