Can't provoke MongoError - bad auth

Viewed 399

Just to be clear, I want to get MongoError bad auth Authentication failed from MongoDB Atlas.
This is about wrong DB password and I'm trying to catch that error and act accordingly.

this is my connection method:

mongoose
  .connect(CRED, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
    useUnifiedTopology: true
  })
  .then(() => {
    console.log('connected to database');
  });

and here I'm catching unhandledRejection error that supposed to catch wrong auth:

process.on('unhandledRejection', err => {
  console.log(err.name, err.message);
  server.close(() => {
    process.exit(1);
  });
});

If I put correct password, everything works fine and I'm connected to database.
But if I put wrong password, after 30 sec of waiting I get:

MongooseTimeoutError Server selection timed out after 30000 ms

...and this shouldn't suppose to work like that.
I should get MongoError (bad auth) immediately, on first attempt when app is connecting to MongoDB
...or am I doing something wrong?

1 Answers

It's known issue for Mongoose <=5.7.1 with useUnifiedTopology: true option.

Update your Mongoose to 5.9.2 to fix the issue.

With new version, Mongoose fails corectly:

MongooseServerSelectionError MongooseError [MongooseServerSelectionError]: Authentication failed.
    at new MongooseServerSelectionError (D:\myapp\node_modules\mongoose\lib\error\serverSelection.js:22:11)
    at NativeConnection.Connection.openUri (D:\myapp\node_modules\mongoose\lib\connection.js:808:32)
    at Mongoose.connect (D:\myapp\node_modules\mongoose\lib\index.js:333:15)
    at Object.<anonymous> (D:\myapp\app.js:46:10)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11 {
  message: 'Authentication failed.',
  name: 'MongooseServerSelectionError',
  reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  },
  [Symbol(mongoErrorContextSymbol)]: {}
}

Also, add .catch clause to your code

mongoose
  .connect(CRED, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
    useUnifiedTopology: true
  })
  .then(() => {
     console.log('connected to database');
  })
  .catch(error => {
     //MongooseServerSelectionError MongooseError [MongooseServerSelectionError]: Authentication failed
     console.log("Error", error)
  });
Related