0

Before anyone says anything, I tried these 2 solutions. It didn't work:

  1. Error at connecting to MongoDb Atlas Server
  2. Node.js and MongoDB Atlas Mongoose Connection Error

So I have a DB on Atlas and I'm trying to connect is from Node JS app using Mongoose. Here is the code:

.env file

MONGODB_URI="mongodb+srv://sidrah:sidrah@cluster0.yidsza6.mongodb.net/dbtodolist?retryWrites=true&w=majority"

index.js file

// IMPORTS 
import express from 'express';
import bodyParser from 'body-parser';
import mongoose from 'mongoose';
import cors from 'cors';
import dotenv from 'dotenv';
import morgan from 'morgan';
import helmet from 'helmet';
// import router from '../routers/routes';

// MIDDLEWARE CONFIGURATIONS
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors());
app.use(helmet());
app.use(helmet.crossOriginResourcePolicy({ policy: "cross-origin"}));
app.use(morgan("common"));
app.use(bodyParser.json({ limit: "30mb", extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb", extended: true }));
// app.use('/api/', router);


// MONGOOSE SETUP
const PORT = process.env.PORT || 6001;
mongoose
.connect(process.env.MONGODB_URI, {
  useNewUrlParse: true,
  useUnifiedTopology: true
})
  .then(()=> {
    console.log('Database connected');
  })
  .catch((error)=> {
    console.log('Error connecting to database');
  });
app.listen(PORT, () => {
  console.log(`Our server is running on port ${PORT}`);
});

My IP address is already whitelisted in Atlast. Plus I've also allowed "Access from Anywhere" from IP Access option. The user that I've created in Database Access is set to Atlas Admin Role.

The Mongoose version is "mongoose": "^6.8.3". Anything else that I'm missing?

Thanks in advance

degr8sid
  • 35
  • 6

0 Answers0