#unable to connect to MongoDb.

38 messages · Page 1 of 1 (latest)

sturdy sorrel
#

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

dire kayak
#

Which one isn't working? The localhost connection will need the port. Usually it is 27017.

dire kayak
#

What error do you get with the mongodb.net URL? I could at least get a "not authorized" from the server.

next wind
sturdy sorrel
#

ok

dire kayak
#

You will probably need to HTML encode any special characters yourself.

sturdy sorrel
# next wind try to set the `useNewUrlParser` to `true`

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

sturdy sorrel
#

i am using the ConfigModule package as on the Nestjs docs

dire kayak
#

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.

dire kayak
#

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.

next wind
next wind
#

The keys are case sensitive

next wind
dire kayak
#

Yeah, URI/URL tomato/ tomato. 🙂 Basically means the same in this context.

sturdy sorrel
next wind
# sturdy sorrel

Buddy of course that console log is gonna print undefined!
Because its bein ran before the nestjs app even initiating

sturdy sorrel
next wind
tropic hollowBOT
#

Can not interact with role <@&918988965095297055>. Please adjust the hierarchy.

dire kayak
#

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.

next wind
#

Or be lazy and reset your password since you've already leaked it
Hope you get one without special characters this time gzKekw

sturdy sorrel
#

works now on mongodb.net. Thanks. just doesnt connect to mongodb community version which connects on my mongodb compass.

carmine tusk
#

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.