varsha kumari
varsha kumari

Reputation: 41

I am using mongoDB atlas and I am getting MongoServerError. When I am using without .env file every thing is fine but with env it is throwing error

    PORT= <port>
    DB_HOST= <host>
    DB_URI= <uri>
    DB_NAME= <db>
    DB_USER= <user>
    DB_PASS= <pass>
    // app.js file
      import express from 'express';
      import mongoose from 'mongoose';
      import fetch from "node-fetch";
      import Project from './project.js';
      import dotenv from "dotenv";
      dotenv.config()
      console.log(dotenv.parsed);
      const app = express();
      mongoose
      .connect(process.env.DB_URI,{
      dbName: 'process.env.DB_NAME',
      user: 'process.env.DB_USER',
      pass: 'process.env.DB_PASS',
     })
     .then((result) => {
      console.log("Database connected successfuly!");
     })
     .catch((err) => {
       console.error(err);
     });

      app.use(express.json());
      app.use(express.urlencoded({ extended: true }));
      app.get('/project', (req, res) => {
      Project.find({}, (err, data) => {
      res.json(data);
      });
      });


       const port= process.env.PORT || 3000

       app.listen(port, () => {
       console.log('starting server at port' + port);
       })


 // Error encountered

 undefined
(node:5659) ExperimentalWarning: stream/web is an experimental feature. This feature    could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
starting server at port3000
MongoServerError: bad auth : Authentication failed.
at MessageStream.messageHandler (/home/varsha/Documents/projects/node_modules/mongodb/lib/cmap/connection.js:467:30)

Please help how to resolve this issue. Thanks in advance at MessageStream.emit (node:events:390:28) at processIncomingData (/home/varsha/Documents/projects/node_modules/mongodb/lib/cmap/message_stream.js:108:16) at MessageStream._write (/home/varsha/Documents/projects/node_modules/mongodb/lib/cmap/message_stream.js:28:9) at writeOrBuffer (node:internal/streams/writable:389:12) at _write (node:internal/streams/writable:330:10) at MessageStream.Writable.write (node:internal/streams/writable:334:10) at TLSSocket.ondata (node:internal/streams/readable:754:22) at TLSSocket.emit (node:events:390:28) at addChunk (node:internal/streams/readable:315:12) { ok: 0, code: 8000, codeName: 'AtlasError' }

Upvotes: 0

Views: 815

Answers (1)

Matt Oestreich
Matt Oestreich

Reputation: 8528

This is because you are passing the env var name as a literal string, not the backing value.

Meaning, if you change this part:

     dbName: 'process.env.DB_NAME',
      user: 'process.env.DB_USER',
      pass: 'process.env.DB_PASS',

to this:

     dbName: process.env.DB_NAME,
      user: process.env.DB_USER,
      pass: process.env.DB_PASS,

as well as a .env file that contains:

NOTE: PORT is optional since you either use the .env value or 3000 (eg. const port = process.env.PORT || 3000)

PORT=1234
DB_URI=mongo://your.uri
DB_NAME=yourdbnamegoeshere
DB_USER=dbuser
DB_PASS=dbpass 

Also, if you are publishing this code to a public repo, make sure you use a .gitignore file with .env in it.

Upvotes: 2

Related Questions