Connection to MongoDB cluster getting failed through NodeJS

I am working on the ExpressJS web services project. We have created a free cluster in MongoDB Atlas and I am trying to connect to the DB using the connection string provided by the website itself. I have whitelisted my own IP and made it accessible from anywhere as well but still nothing has worked yet. To what I think my code is okay as I was able to connect to the localhost database. But I am still mentioning it below for better review.

const bodyParser = require('body-parser')
const cors = require('cors')
const mongoose = require('mongoose');

const app = express();
const port = 8000;

// Encode username and password for mongodb as URI encoded component
const username = encodeURIComponent("<username>");
const password = encodeURIComponent("<password>")

// const mongoDbUrl = `mongodb://${username}:${password}@host:port/database`;
// const mongoDbUrl = `mongodb://${username}:${password}@diversity-of-living-clu.hqwcott.mongodb.net`;
const mongoDbUrl = "mongodb://<username>:<password>@diversity-of-living-clu.hqwcott.mongodb.net/"

// mongodb://admin:[email protected]/3380DiversityOfLiving-data?retryWrites=true&w=majority&appName=diversity-of-living-cluster
// Initialising options for cors
const corsOptions = {
    origin: "*",
    credentials: true
}

app.use(bodyParser.json());
app.use(cors(corsOptions));

// Routers
const numbeoRouter = require('./DiversityOfLiving-Routers/NumbeoRouterAra65');
const userRouter = require('./DiversityOfLiving-Routers/UserRouterJpa72');
const faqRouter = require('./DiversityOfLiving-Routers/FAQRouterJpa72');
const countryCountRouter = require('./DiversityOfLiving-Routers/CountryCountRouterJpa72');
const countryDataRouter = require('./DiversityOfLiving-Routers/CountryDataRouterAra65');

// Using Routers
app.use('/numbeo', numbeoRouter);
app.use('/faq', faqRouter);
app.use('/countrycount', countryCountRouter);
app.use('/user', userRouter);
app.use('/countrydata', countryDataRouter);

app.listen(port, function () {
    console.log('Express server listening on: http://localhost:' + port);
    mongoose.connect(mongoDbUrl, { useNewUrlParser: true, useUnifiedTopology: true, connectTimeoutMS: 10000 })
    .then(() => {console.log("Connected to MongoDB");})
    .catch((err) => {console.log("Error connecting to MongoDB:", err.message, err.stack);})
});```



The solutions that I have tried: 

- Both SRV and Non-SRV URLs
- Whitelisted my IP and made it available from anywhere as well.

The below is the error that I am getting: 


Error connecting to MongoDB: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/ MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/


But I do not think that is the issue as I was able to connect to the cluster using my MongoDB Compass, Mongosh and VS Code MongoDB extension.

Open to any reviews to the code and to try new solutions as well. Thank you!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật