Q: Can not connect Node.js app with MongoDB Atlas I have created a new cluster in MongoDB Atlas, but I can not connect my node.js app with it. I am new to the backend and I am learning MongoDB.
Error: querySrv ENODATA _mongodb._tcp.cluster0.onw2w.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19) {
    errno: undefined,
    code: 'ENODATA',
    syscall: 'querySrv',
    hostname: '_mongodb._tcp.cluster0.onw2w.mongodb.net'
}

The server is running successfully. My app was working before connecting to Atlas. I created a new cluster from the beginning again so that I can make sure that I made no mistake while creating the cluster.
server.js file
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const app = require('./app');
dotenv.config({ path: './config.env' });

const DB = process.env.DATABASE.replace(
  '<PASSWORD>',
  process.env.DATABASE_PASSWORD
);

mongoose
  .connect(DB, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
    useUnifiedTopology: true,
  })
  .then((con) => console.log('DB connection successful....'))
  .catch((error) => console.log(error));

const tourSchema = new mongoose.Schema({
  // to specify a schema for our model
  name: {
    type: String,
    require: [true, 'A tour must have a name'], // validator
    unique: true,
  },
  rating: {
    type: Number,
    require: 4.5,
  },
  price: {
    type: Number,
    require: [true, 'A tour must have a price'],
  },
});

const Tour = mongoose.model('Tour', tourSchema);

const testTour = new Tour({
  // it is an instance of the tour model
  name: 'The forest Hiker',
  rating: 4.7,
  price: 497,
});

testTour
  .save() // this will save the data in the Tour collection in the database it will return a promise
  .then(doc => console.log(doc))
  .catch(err=> console.log('error: ', err))

const port = process.env.PORT || 8000;
app.listen(port, '127.0.0.1', () => {
  console.log(`server running on port ${port}.......`);
});

.env file
NODE_ENV=devlopment
PORT=8000
USERNMAE=IrfanAsif
DATABASE_PASSWORD=zAC8yIHLJIegJHSQ
DATABASE=mongodb+srv://irfan:<PASSWORD>@cluster0.onw2w.mongodb.net/myFirstDatabase?retryWrites=true&w=majority


A: so first of all I don't see any particular reason to keep the password outside of DB connection string, since it is in .env variable, so it won't be by any chance avalible to end user, or anyone without access to server files.
To your problem, could you log proccess.env.DATABASE and the password to console, to make sure it is priting it out correctly ? Since your callback, and promise resolve seem to be allright, it should be configuration issue.
