Nodejs Mongoose Error: getaddrinfo ENOTFOUND undefined undefined:27017
<p>I'm trying to switch to mongoose from using just the native mongodb and having some trouble.</p>

<p>Connecting to the db as I did previously works fine:</p>

<pre><code>var db;

var passDb = function(req, res, next) {
    if (!db) {
        mongo.connect(process.env.MONGOLAB_URI, function(err, database) {
            if (err) throw err;
            db = database;

            req.db = db;
          next();
        });
    } else {
      req.db = db;
      next();
    }
}
</code></pre>

<p>But the way I'm trying to do it now throws an error:</p>

<pre><code>var mongoose     = require('mongoose');

var Poll         = require('./app/models/poll');

mongoose.connect(process.env.MONGOLAB_URI);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  // we're connected!
});
</code></pre>

<p>The error:</p>

<pre><code>connection error: { [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
]
  name: 'MongoError',
  message: 'getaddrinfo ENOTFOUND undefined undefined:27017' }
</code></pre>

<p>Any ideas what is causing this?</p>

<p>edit: it works when I use the database uri directly, but not when I use the environment variable. I have checked and triple checked that everything is typed correctly, it refuses to use the environment variable and I have to put it directly in the code :S</p>