MongoDb Atlas not connecting to Heroku
By default process.env.NODE_ENV
is production
on Heroku, so it won't use .env file even if it is under the project folder.
Adding MONGO_URI
and your mongodb connection string
key value pair in Heroku should do the trick:
- login Heroku
- select your app
- click on settings tab
- find 'Config Vars' section
- click 'Reveal Config Vars'
- add
MONGO_URI
as KEY andyour connection string(no quote)
as VALUE - click on add button
- open your app again
Update:I created a demo using your above code and deployed on Heroku following above steps after connecting it to my git repo. It works fine.
You can also check the node console by clicking More
-> View logs
in the Heroku app page(top right).