TransWikia.com

how do i connect my mongodb to my laptop?

Stack Overflow Asked by Omojuwon Soneye on December 1, 2020

I made a mongodb account , and tried connecting it with my laptop. it has been giving me this error

here is 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)

here is the error

server started on port 5000
MongoError: Authentication failed.
at MessageStream.messageHandler (C:UsersJUWONCALEB.DESKTOP-CMA289UDesktopUDEMYECOMMERCEbackendnode_modulesmongodblibcmapconnection.js:268:20)
at MessageStream.emit (events.js:314:20)
at processIncomingData (C:UsersJUWONCALEB.DESKTOP-CMA289UDesktopUDEMYECOMMERCEbackendnode_modulesmongodblibcmapmessage_stream.js:144:12)
at MessageStream._write (C:UsersJUWONCALEB.DESKTOP-CMA289UDesktopUDEMYECOMMERCEbackendnode_modulesmongodblibcmapmessage_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'
}

2 Answers

I noticed you're using strings here '' and not back tics ``. So that was probably the reason why you were getting that error. Another probability is that you maybe have configured your env in a wrong way. Check if you env is working by using console logging it.

Finally, I suggest you put the whole mongoDB Connection as an env and then just use the env without having to worry about strings. For example:

inside your .env file:

DATABASE_CONNECTION=mongodb+srv://the-user:[email protected]/your-database?authSource=yourDB&w=1

then just do this

mongoose.connect(
  process.env.DATABASE_CONNECTION,
    {
        useUnifiedTopology: true,
        useNewUrlParser: true
    }).then(() => {
        console.log("database connected");
    }).catch((err) => console.log(err));
app.use(bodyParser)

Answered by MK. on December 1, 2020

You have to use Backticks when interpolating strings.

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)

Answered by Harshana Serasinghe on December 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP