I have deployed my nodejs project to Heroku and have used the mongodb cluster url but I also want to work in my local machine and use the local host.
I tried writing this code for when it doesn't connect to the cluster it should connect to the local host but the problem is the atlas url doesn't return 'undefined' when not connected.So how to connect with both the atlas cluster and the local host.
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
require('dotenv').config();
const mlabDB = `mongodb+srv://${process.env.MLAB_USERNAME}:${process.env.MLAB_PASSWORD}@todo-app-qhj7g.mongodb.net/test?retryWrites=true&w=majority`;
mongoose.connect(mlabDB || 'mongodb://localhost:27017/TodoApp', {useNewUrlParser: true})
.catch((e) => {
console.log(e);
});
module.exports = {mongoose};