0

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!

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
Gow Tham
  • 1
  • 7
  • If those are your actual login details, you should consider your database compromised. – Julia Jan 04 '23 at 11:07
  • Yes, but the data base is not for commercial use and this is from my other account. The actual account details are different from the above i mentioned. By the way, can u spot what's wrong with my code? Thanks for ur time! – Gow Tham Jan 04 '23 at 11:11
  • Maybe this one: https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 – Wernfried Domscheit Jan 04 '23 at 11:28
  • It is a bit different from my problem. My code works but not in the case when i use the url from the .env file. Thanks for the time. – Gow Tham Jan 04 '23 at 11:37
  • Are you sure your login details are correct? I've tried, because you said that there are no important data, with pass in original question (that before your edits) – pierpy Jan 04 '23 at 13:33

0 Answers0