Jacob Broughton
Jacob Broughton

Reputation: 422

Unable to connect to MongoDB upon deployment / production

My mongoDB connects just fine locally, then once I deploy to heroku, the connection URL becomes undefined. I have a .env file in my root directory, and a .gitignore which includes the .env. I need the mongo url to stay private, but I can only assume that this is why it shows as undefined in production...

Heres the error: "MongooseError: The uri parameter to openUri() must be a string, got "undefined". Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string."

Heres my require: require("dotenv").config() var url = process.env.MONGODB_URI

Heres my connection: mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }).catch(e => { console.error(e.message) })

Thanks in advance for the help.

Upvotes: 1

Views: 1007

Answers (2)

Noob
Noob

Reputation: 2807

You need to setup your variables where your app is deployed. It is not accessible from your .env file that you used while developing on your local host. If you deployed to heroku - here is a documentation of what to do. If it's not deployed to heroku, you can get an idea what to do next to make it work.

Upvotes: 0

Majid Rehman
Majid Rehman

Reputation: 161

Two things you need to do, Make that .env available for the production code, It can't access your local .env file.

Second is your mongo running on production ? mean availabe and accessible for the app through 27017 port ? don't say it is running on your local machine.

Upvotes: 1

Related Questions