#API routes not working after deploying on vercel

47 messages ยท Page 1 of 1 (latest)

latent zephyr
#

Can someone tell me why my api routes were working in local but not in vercel ?

dull anchorBOT
#

๐Ÿ”Ž 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)

latent zephyr
#

hello?

gusty dragon
#

your api return 500 error check server logs

latent zephyr
wheat merlin
latent zephyr
wheat merlin
#

You can search on the logs tab

latent zephyr
#
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
wheat merlin
#

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.

latent zephyr
#

if env vars were wrong , wouldnt it also cause issue on local ?

wild ledge
#

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

latent zephyr
#

I changed my callback url too

wheat merlin
#

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

latent zephyr
#

should I remove my env file from github repo ?

sonic glade
#

Just bake the secrets into the code

#

.env files are a scam

wild ledge
wheat merlin
#

based on this chat, yes just remove them.

latent zephyr
#

i removed them and its the same . i kinda want to share my screen and solve this ๐Ÿ˜ฆ

wheat merlin
#

have you put the env values into the vercel portal,

latent zephyr
wheat merlin
#

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?

latent zephyr
#

I copy pasted everything directly into the portal , there is no way , it messes it up

latent zephyr
#

DAY 2 of seeking help

#

@spring vapor help ;-;

wraith apex
#

getsession does an extra fetch that's uneccary

feral heathBOT
# latent zephyr <@743561772069421169> help ;-;
Don't ping or DM other devs you aren't actively talking to

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

latent zephyr
wraith apex
#

likely

#

check your setup

latent zephyr
# wraith apex yeah
signIn<P>(
   provider?, 
   options?, 
authorizationParams?): Promise< P extends RedirectableProviderType ? SignInResponse | undefined : undefined >``` can you turn this into js ? ๐Ÿ˜ฆ
crystal fractal
#

@latent zephyr same problem here... have you solved your problem?

jade cradle
#

@latent zephyr Same problem here. Did you find any solution?

crystal fractal