0

Hi all so I went through the MERN Stack tutorial: https://www.mongodb.com/languages/mern-stack-tutorial.

Am not able to connect successfully to MongoDB.

Server is running on port: 8080
MongoServerError: bad auth : Authentication failed.
    at Connection.onMessage (/Users/username/Documents/Projects/projectname/server/node_modules/mongodb/lib/cmap/connection.js:227:30)
    at MessageStream.<anonymous> (/Users/username/Documents/Projects/projectname/server/node_modules/mongodb/lib/cmap/connection.js:60:60)
    at MessageStream.emit (node:events:513:28)
    at processIncomingData (/Users/username/Documents/Projects/projectname/server/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
    at MessageStream._write (/Users/username/Documents/Projects/projectname/server/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
    at writeOrBuffer (node:internal/streams/writable:392:12)
    at _write (node:internal/streams/writable:333:10)
    at Writable.write (node:internal/streams/writable:337:10)
    at TLSSocket.ondata (node:internal/streams/readable:766:22)
    at TLSSocket.emit (node:events:513:28) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError',
  connectionGeneration: 0,
  [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }

The 'config.env' file contains this:

ATLAS_URI=mongodb+srv://correctusername:correctpassword@atlascluster.zgubjgl.mongodb.net/employees?retryWrites=true&w=majority
PORT=8080

The 'conn.js' contains:

const { MongoClient } = require("mongodb");
const Db = process.env.ATLAS_URI;
const client = new MongoClient(Db, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
 
var _db;
 
module.exports = {
  connectToServer: function (callback) {
    client.connect(function (err, db) {
      // Verify we got a good "db" object
      if (db)
      {
        _db = db.db("employees");
        console.log("Successfully connected to MongoDB."); 
      }
      return callback(err);
         });
  },
 
  getDb: function () {
    return _db;
  },
};

Solutions attempted:

  1. Ensured that the username and password do not have '<>' enclosing them.
  2. Created a new user in 'Database Access' in MongoDB itself with 'readWriteAnyDatabase@admin' access to mirror the ending of the ATLAS_URI connection string, same error.
  3. Removed 'employees' in connection string and tried again, same error.
  4. Tried another database name in both 'config.env' and 'conn.js' files, same error.
  5. Ensured that my 'Network Access' in MongoDB itself is '0.0.0.0/0 (includes your current IP address)'.
  6. Ensured that cluster name matches.

All of the solutions above are things I tried based off what I have researched on Stack. Is the problem that I don't have an employees database set up on MongoDB itself? Or is it a security setting on my Mac that is causing an issue?

Please help - feeling very downtrodden that I have reached this obstacle as I have a really good platform idea to start a business based on.

0 Answers0