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
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:
require("dotenv").config();
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
console.error(e);
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)?
add your database’s connection string (for example: mongodb+srv://:@cluster0-jvwxi.mongodb.net/?retryWrites=true&w=majority ) to the environment variable MONGO_URI .
If you have done that, then it might be worth a try commenting out the require("dotenv").config() code.
It is difficult to help with the environment variable stuff, because we cannot see your secrets on Replit, unless you specifically invite us to, which you probably should not.
Probably not related as you have already worked with mongodb before this, but just in case, did you whitelist the IPs? Adding 0.0.0.0/0 allows access from anywhere.
A few of these mistakes would be caught by a modern text editor. I suggest you switch to using either a local editor, or something more feature rich like CodeSandbox.
I wonder if this error is the reason it keeps failing that database connection test:
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)
Yes. that means that you haven’t set the connection string in the .env file correctly (if you hadn’t set it at all, I think it would be undefined instead of null).