So down below is the snippet I use to connect to my database 'journal'. I placed the password within the string itself. The issue is, that whenever use the URL from the process.env
I get the Bad auth err
. But if I use the original plain string, it works perfectly. I can perform read, del, write and update operations easily, but if I use the URL from the .env
file the connection fails.
require('dotenv').config()
const Port = process.env.port
const url = process.env.DB_conn
//'mongodb+srv://gotham:password@journal.4ok3oge.mongodb.net/journal?retryWrites=true&w=majority'
mongoose.set('strictQuery', false)
try {
mongoose.connect(url,{
useNewUrlParser : true
})
}
catch(err) {
console.log("connectoin failed")
}
And down below is my .env
file:
DB_conn = 'mongodb+srv://gotham:password@journal.4ok3oge.mongodb.net/journal?retryWrites=true&w=majority'
port = 5000
Why does this happen with just the env vars, is anything wrong with the way I access the vars in the .env
file?
I have seen different approaches like using options but those were for connecting to a locally hosted database, so I did not go with that approach. Please help me out!