MongoError: Authentication Failed

What’s happening:
I have this error:

MongoError: Authentication failed.
    at MessageStream.messageHandler (/home/runner/boilerplate-advancednode/node_modules/mongodb/lib/cmap/connection.js:268:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (/home/runner/boilerplate-advancednode/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
    at MessageStream._write (/home/runner/boilerplate-advancednode/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
    at doWrite (_stream_writable.js:403:12)
    at writeOrBuffer (_stream_writable.js:387:5)
    at MessageStream.Writable.write (_stream_writable.js:318:11)
    at TLSSocket.ondata (_stream_readable.js:717:22)
    at TLSSocket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:295:12) {
  ok: 0,
  code: 8000,
  codeName: 'AtlasError'
(node:129) UnhandledPromiseRejectionWarning: Error: Unable to Connect to Database

My code so far

"use strict";
const express = require("express");
const myDB = require("./connection");
const fccTesting = require("./freeCodeCamp/fcctesting.js");
const session = require("express-session");
const passport = require("passport");
const ObjectID = require("mongodb").ObjectID;

const app = express();

fccTesting(app); //For FCC testing purposes
app.use("/public", express.static(`${process.cwd()}/public`));

app.set("view engine", "pug");

app.use(express.urlencoded({ extended: true }));

  secret: process.env.SESSION_SECRET,
  resave: true,
  saveUninitialized: true,
  cookie: { secure: false }


app.route("/").get((req, res) => {
  res.render(`${process.cwd()}/views/pug/index`, {
    title: "Hello", message: "Please login"

myDB(async client => {
  const usersDB = await client.db("usersDB").collection("users");

  app.route("/").get((req, res) => {
    res.render("pug", {
      title: "Connected to Database",
      message: "Please login"

  passport.serializeUser((user, done) => {
    done(null, user._id);

  passport.deserializeUser((id, done) => {
    myDatabase.findOne({ _id: new ObjectID(id) }, (err, doc) => {
      done(null, doc);
  }).catch(e => {
    app.route("/").get((req, res) => {
      res.render("pug", {
        title: e,
        message: "Unable to login"

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Listening on port ${PORT}`);

My browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36 Edg/90.0.818.42.

Challenge: Implement the Serialization of a Passport User

Link to the challenge:

Any help would be appreciated. Thanks in advance.

I surrounded the value for MONGO_URI in double quotes in the Secrets file on Repl and it made the “Authentication failed” error go away.

But I still have two other problems:

TypeError: Cannot read property 'split' of null
    at parseSrvConnectionString (/home/runner/boilerplate-advancednode/node_modules/mongodb/lib/core/uri_parser.js:44:23)
    at parseConnectionString (/home/runner/boilerplate-advancednode/node_modules/mongodb/lib/core/uri_parser.js:587:12)
    at connect (/home/runner/boilerplate-advancednode/node_modules/mongodb/lib/operations/connect.js:282:3)
    at /home/runner/boilerplate-advancednode/node_modules/mongodb/lib/mongo_client.js:223:5
    at maybePromise (/home/runner/boilerplate-advancednode/node_modules/mongodb/lib/utils.js:662:3)
    at MongoClient.connect (/home/runner/boilerplate-advancednode/node_modules/mongodb/lib/mongo_client.js:219:10)
    at main (/home/runner/boilerplate-advancednode/connection.js:11:22)
    at Object.<anonymous> (/home/runner/boilerplate-advancednode/server.js:36:1)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
Listening on port 8080
(node:290) UnhandledPromiseRejectionWarning: Error: Unable to Connect to Database

I noticed that the “Unable to Connect to Database” error message is from here:

const { MongoClient } = require("mongodb");

async function main(callback) {
    const URI = process.env.MONGO_URI; // Declare MONGO_URI in your .env file
    const client = new MongoClient(URI, { useNewUrlParser: true, useUnifiedTopology: true });

    try {
        // Connect to the MongoDB cluster
        await client.connect();

        // Make the appropriate DB calls
        await callback(client);

    } catch (e) {
        // Catch any errors
        throw new Error("Unable to Connect to Database")

module.exports = main;

in connection.js. I need help in figuring where the other error is coming from. It seems to be from the uri_parser library, but I don’t see that library being required in server.js. Maybe it’s being required through one of the libraries there as a dependency.

Could someone help me out?

Also, which of the libraries being required in server.js are the likeliest to be using uri_parser as a dependency (asking so I can narrow it down)?