title: How to connect heroku to MongoDB Atlas
body: <p>I've been having a complete nightmare, trying to connect my heroku app to MongoDB Atlas, as mLab has now been deleted as a Heroku addon.</p>
  <p>The problem I'm having, is that I can't seem to connect my app, to my Atlas DB.</p>
  <ol>
  <li>I've created a cluster in Atlas.</li>
  <li>Set my cluster IP address to <code>0.0.0.0/0</code></li>
  <li>Updated my Config Vars in Heroku.</li>
  </ol>
  <p>It seems as if my app is working fine locally...</p>
  <p>To add some complexity, a couple of years ago, I could run mongoose locally, just by running the <code>mongod</code> command.  However, this seems to have stopped working, because of the the MacOS Catalina update.... My terminal couldn't <code>/data/db</code> directory:</p>
  <p><a href="https://stackoverflow.com/questions/58283257/mongodb-cant-find-data-directory-after-upgrading-to-mac-os-10-15-catalina">MongoDB can&#39;t find data directory after upgrading to Mac OS 10.15 (Catalina)</a></p>
  <p>So I followed instructions to create a new <code>/data/db</code> folder.  This works locally, if I run:
  <code>mongod --dbpath=/Users/reenaverma/MongoData</code></p>
  <p>So locally, I can use my app and update the DB fully.</p>
  <p>However, when I access my app in PROD/heroku, every time I try to login or perform REST/DB actions, (such as a get request), I get a 503 error.  So I know, my DB isn't connected in PROD/heroku.</p>
  <p>I'm running out of ideas, as to what I'm missing.</p>
  <p>My <code>mongo --version</code> is MongoDB shell version v3.6.2.</p>
  <p>Here's how my index.js looks.  Any ideas, would be much appreciated!!</p>
  <pre><code>const express = require('express');
  const expressLayouts = require('express-ejs-layouts');
  const bodyParser = require('body-parser');
  const methodOverride = require('method-override');
  const mongoose = require('mongoose');
  mongoose.Promise = require('bluebird');
  const router = require('./config/router');
  const session = require('express-session');
  const flash = require('express-flash');
  const userAuth = require('./lib/userAuth');
  
  
  const app = express();
  const PORT = process.env.PORT || 8000; 
  
  // CONNECT TO THIS DATABASE
  mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/music-database', { useNewUrlParser: true, useUnifiedTopology: true });
  mongoose.set('useCreateIndex', true);
  
  // SET EJS USE
  app.set('view engine', 'ejs');
  app.set('views', `${__dirname}/views`);
  app.use(expressLayouts);
  app.use(express.static(`${__dirname}/public`));
  
  
  // ENSURES FORM DATA IS LISTENED TO AND CAPTURED
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use(methodOverride(req =&gt; {
    if(req.body &amp;&amp; typeof req.body === 'object' &amp;&amp; '_method' in req.body) {
      const method = req.body._method;
      delete req.body._method;
      return method;
    }
  }));
  
  // SETTING UP SESSION COOKIE
  app.use(session({
    secret: 'xxxx(', // a random key used to encrypt the session cookie
    resave: false,
    saveUninitialized: false
  }));
  
  // setup flash messages must be AFTER express-session
  app.use(flash());
  
  app.use(userAuth);
  app.use(router);
  
  // GLOBAL ERROR CATCHER FOR 422 and 500
  app.use((err, req, res, next) =&gt; {
    if(err.name === 'ValidationError') return res.render('pages/422');
    res.render('pages/500', { err });
    next(err); //display error in terminal/also a callback function
  });
  
  // PORT LISTENER - MUST BE AT BOTTOM
  app.listen(PORT, () =&gt; console.log(`Up and running on port ${PORT}`));
  </code></pre>
  <p>And here's how my Config Vars looks, in Heroku:</p>
  <p><a href="https://i.stack.imgur.com/KaXP3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KaXP3.png" alt="enter image description here" /></a></p>