0

I've build a nest.js server and now I'm trying to connect mongoDB Atlas. In the app.module imports i've tried to connect with

mongodb+srv://<user>:<pass>@cluster0.iijhz.mongodb.net/<dbName>?retryWrites=true&w=majority

url string, but failed, now i've tried these options too:

imports: [
MongooseModule.forRoot(
  'mongodb+srv://cluster0.iijhz.mongodb.net',
  {
    user: '<user>',
    pass: '<pass>',
    dbName: '<dbName>',
    w: 'majority',
    retryWrites: true
  }
),

but still got

MongoError: Authentication failed.

my ip, and 0.0.0.0/0 is also added to whitelist.

  • Probably this can help: https://stackoverflow.com/a/20809536/4980141 – Haniel Baez Jan 20 '21 at 14:13
  • Thanks, i've read the docs, and some articles about nest's MongooseModule, then read the docs for MongoDB Atlas, changed my password several times, and just after that all, checked the name of the db, which was wrong. – Péter Ciprián Oláh Jan 20 '21 at 14:40

2 Answers2

0

Ok, surely my bad, the dbName wasn't correct the whole time.

0

The problem that I faced was not whitelisting my IP address. The error was not visible when tried with NestJS but while using it with express I found out the error!

In case you're wondering how to change IP address in MongoDB atlas:

  1. Goto MongoDB atlas dashboard

  2. Select Network Access option under Security

  3. In the IP address tab, clik edit on the first(according to your requirement) IP address.

  4. Click Add Current IP address(for only your current IP, you can't access from other IP) / select "Global IP" or 0.0.0.0/0 for getting access from anywhere.

  5. It should work now!

Hope that helps!

Naman Barkiya
  • 159
  • 1
  • 4