#unable to connect to MongoDb.
38 messages · Page 1 of 1 (latest)
Dev Connection string mongodb://localhost/veegil_backend/
Production Connection String mongodb+srv://admin:<password>@cluster0.chejy.mongodb.net/?retryWrites=true&w=majority i know i have to put in the password and i did just that
no connection is being made
Which one isn't working? The localhost connection will need the port. Usually it is 27017.
both
What error do you get with the mongodb.net URL? I could at least get a "not authorized" from the server.
try to set the useNewUrlParser to true
ok
You will probably need to HTML encode any special characters yourself.
did not work. just logged out my env variables and they are undefined, i have them present in my .env file and i followed the Config documentation on the Nestjs website, somehow my .env variables still return indefined
ok
i am using the ConfigModule package as on the Nestjs docs
What I do is use Compass to try and connect to the db. If that works, then I know I did something wrong putting the URL together in the app.
ok great. note taken
So, usually you piece together the URL. username and password should be separate env vars as is the database name. In that "piecing together" you can also encode the username and password to be sure they are URL ready.
Is the .env file located on the root of the project?
yea
ok
Btw isn't it called database "uri"? Idk why on nestjs packages we have them as "url"
Yeah, URI/URL tomato/ tomato. 🙂 Basically means the same in this context.
Buddy of course that console log is gonna print undefined!
Because its bein ran before the nestjs app even initiating
wow ok im new to Nestjs how do i solve this
There's nothing to solve 
If you move the console log inside of the use factory function, you'll see it properly loads the env variable
alright thanks
Can not interact with role <@&918988965095297055>. Please adjust the hierarchy.
Sorry, I meant URL encoding not HTML encoding. 🙂 That password, for instance, should look like this
%24JWR7NK3L%3FrG2u%23
So, once you fix your .env / config issue, the password needs the URL encoding too.
ok
Or be lazy and reset your password since you've already leaked it
Hope you get one without special characters this time 
works now on mongodb.net. Thanks. just doesnt connect to mongodb community version which connects on my mongodb compass.
I had the same exact problem today. After spending hours, I solved it by replacing localhost with 127.0.0.1
mongodb://127.0.0.1:27017/test_db
Also, make sure to set the right NODE_ENV variable, probably in your package.json file.
"start:dev": "cross-env NODE_ENV=development nest start --watch"
where cross-env is a package you'd have to install but it works without it too.