I'm trying to set up mongodb for heroku and I can successfully connect if i put the url in the script normally. However, when I retrieve the url from environmental variables locally to hide it, I get the error 'invalid schema, expected mongodb'.
It's returning the exact same url in the format var url = 'mongodb://username:password@ds139619.mlab.com:39619/food'
Process.env isn't asynchronous is it? I can print the url correctly before the connect fails. I also checked the type and it is a string too.
//var url = 'mongodb://<username>:<password>@ds139619.mlab.com:39619/food';
var url = process.env.MONGOLAB_URI;
var ok = process.env.MONGOLAB_URI;
console.log(ok);
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', url);
// do some work here with the database.
//Close connection
db.close();
}
});
I solved the problem by removing the quotations marks I used when saving the environment variables