0

I would like to replace, in my getStaticProps, getStaticPaths and API routes, all mongodb connection URI strings by environment variables

From :

const client = await MongoClient.connect(
  'mongodb+srv://XXX:XXX@cluster0.uqztxfp.mongodb.net/meetups?retryWrites=true&w=majority'
)

To:

const client = await MongoClient.connect(`${process.env.MONGODB_URI}`)

With MONGODB_URI coded like that in .env.local

DB_USER='XXX'
DB_PASS='XXX'
MONGODB_URI='mongodb+srv://$DB_USER:$DB_PASS@cluster0.uqztxfp.mongodb.net/meetups?retryWrites=true&w=majority'

It perfectly works on getStaticProps and api routes, but not with getStaticPaths where variable is undefined...

All documentation I have read says that .env are accessible from server throught getStaticPaths as well as getStaticProps and api folder.

Notice that the app perfectly works with hard coded string

Thanks in advance

Keitaro
  • 93
  • 2
  • 11

1 Answers1

1

Done. Just have to delete the .next folder and restart my dev server.

I didn't do it before because I had the message bellow in my terminal on each save and env variables where ok in other methods without any other actions, only in getStaticPaths.

Info  - Loaded env from C:\...\.env.local 

Strange but ok :)

Keitaro
  • 93
  • 2
  • 11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 14 '23 at 21:13