#Fetch API configuration

35 messages · Page 1 of 1 (latest)

velvet flare
cobalt ember
#

Why not just create a helper util that does that?

velvet flare
cobalt ember
#

As in, you don't want it in committed in code?

velvet flare
cobalt ember
#

Then, use one

cobalt ember
#

How so?

velvet flare
#

the variable is undefined if i try it

cobalt ember
#

Where did you try and how?

#

How did you define your env variable?

velvet flare
#

in my test instance, I tried it just like in the stackoverflow example:

const res = await fetch(`${process.env.ROOT}/api/users`)

(in a get ServerSideProps)

and I defined in the next.config.js:

module.exports = {
  env: {
    ROOT: process.env.NODE_ENV === 'production' ? `https://vercel.com/myaccount/myappname` : `http://localhost:3000`
  }
};
cobalt ember
#

Hmmm, it shouldn't be undefined

#

Can you try a different env name, maybe be more verbose

#

Also, how did you verify it is undefined?

velvet flare
cobalt ember
#

Can you share please?

#

What are you expecting to happen?

#

Or I should say, what is the expected full url to fetch?

velvet flare
#

so my nextjs app is on localhost:3000

#

and the api on localhost:8102

#

and it just added the fetch path to the nextjs app url and not the api url configured in the variable

cobalt ember
#

Should you not use localhost:8102 in your config?

#

The config you shared has port 3000

velvet flare
cobalt ember
#

I see localhost:3000, not localhost:8102

#

Or am I blind? lol

velvet flare
#

wait i will send my files

cobalt ember
#

Show me, your config please

velvet flare
#

hm i did not change anything and now it works

#

lol

cobalt ember
#

🙂

velvet flare
#

but ty :)

cobalt ember
#

magic