Can Dirik Can Dirik - 6 days ago 5
Node.js Question

How do i connect online database with set NODE_ENV=production && nodemon?

I have 2 databases. 1 of them on local. And other one on mlab.com. When i start app with nodemon, i can connect local database. But when i want to connect database that on mlab. It comes out with problem. I use this :

set NODE_ENV=production && nodemon.


and after this i still get local connection. And this code is in db.js file.

var mongoose = require('mongoose');
var dbURI = 'mongodb://localhost/WiFiBuddy';
if (process.env.NODE_ENV === 'production') {
dbURI = process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);


and by the way i can connect to online database with mongoshell. But can't connect with command line.

dan dan
Answer

According to an issue on the nodemon Github repo, the space after "production " could be causing the issue. So if you use:

set NODE_ENV=production&& nodemon.

Then the production if statement should be triggered