0

I am trying to deploy my first app to heroku using mLab MongoDB addon. How should I define the connection?

This is what I found in a NodeJS book, it does not seem to work, though. I replaced the password and database name with appropriate values.

mongoose.connect('mongodb://databasename:[email protected]:10089/app26');
mongoose.connection.on('open', function() {
    console.log('Mongoose connected.');
});

1 Answer 1

1

You should use Heroku enviroment variables. When you add a Mongo addon you should automatically have the enviroment variable available (these can also be accessed through the Heroku dashboard). So with MLAB you can get your connection string by accessing process.env.MONGOLAB_URI. So you can just do mongoose.connect(process.env.MONGOLAB_URI);

To get this to work on your localost you run heroku config > .env to export the enviroment variables to a .env file at the root of your app. Then when you run heroku local the MONGOLAB_URI env variable will be also available on your localhost.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.