I am using .env file to store mongodb uri but i am not able to use it inside my db.js. Here are the screenshots of code.
db.js
db.js screenshot
.env
.env screenshot
I am getting this error when i am executing this, instead of this i expect to get no error and connect with my mongodb database.
enter image description here
2
Answers
Put the URL address in the .env file inside double quotes. Like the following code:
and run the dotenv module as a single line before calling by process:
Your
uri
variable isundefined
because you are creating it before you call theconfig()
function soprocess.env.URI
is not in scope yet. That’s why your MongoDB connection error is being thrown.Simply change the order or execution like so:
.env