I recently created a MongoDB account and attempted to connect it to my laptop, but unfortunately I encountered an error during the process.
Below is the code snippet for my Mongoose connection:
mongoose.connect(
'mongodb+srv://${process.env.MONGO_DB_USER}:${process.env.MONGO_DB_PASSWORD}@cluster0.atfhj.mongodb.net/${process.env.MONGO_DB_DATABASE}?authSource=yourDB&w=1',
{
useUnifiedTopology: true,
useNewUrlParser: true
}).then(() => {
console.log("database connected");
}).catch((err) => console.log(err));
app.use(bodyParser)
Upon connecting, I encountered the following error message:
server started on port 5000
MongoError: Authentication failed.
at MessageStream.messageHandler (C:\Users\JUWONCALEB.DESKTOP-CMA289U\Desktop\UDEMY\ECOMMERCE\backend\node_modules\mongodb\lib\cmap\connection.js:268:20)
at MessageStream.emit (events.js:314:20)
at processIncomingData (C:\Users\JUWONCALEB.DESKTOP-CMA289U\Desktop\UDEMY\ECOMMERCE\backend\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
at MessageStream._write (C:\Users\JUWONCALEB.DESKTOP-CMA289U\Desktop\UDEMY\ECOMMERCE\backend\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:719:22)
at TLSSocket.emit (events.js:314:20)
at addChunk (_stream_readable.js:298:12)
at readableAddChunk (_stream_readable.js:273:9)
at TLSSocket.Readable.push (_stream_readable.js:214:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:188:23) {
ok: 0,
code: 8000,
codeName: 'AtlasError'
}