title: Node, Express, and Mongoose: Authentication failed error
body: <p>I'm new to Node/Express/Mongo/MLab and backend programming in general. I am trying to submit a post request to an MLab database and running into trouble. I think my problem is with Mongoose and MLab.</p>
  
  <p><a href="https://glitch.com/edit/#!/imminent-crime?path=server.js:88:4" rel="nofollow noreferrer">Here is my Glitch project</a></p>
  
  <p>I'm getting error <code>MongoDB connection error: { MongoError: Authentication failed.</code> Why am I getting this error? Is there a problem with my <code>mongoose.connect</code> function and credentials? Is MLab not properly set up?</p>
  
  <pre><code>'use strict';
  
  var express = require('express');
  var mongo = require('mongodb');
  var mongoose = require('mongoose');
  var bodyParser = require('body-parser');
  var validUrl = require('valid-url');
  var dns = require("dns");
  var cors = require('cors');
  var app = express();
  
  // Basic Configuration 
  var port = process.env.PORT || 3000;
  
  
  // Hooks up app to MLab MongoDB database by using the .env variable I created
  // Doesn't work. Data is not submitted to MLab and /api/shorturl/new endpoint freezes up when form is submitted
  mongoose.connect(process.env.MONGODB_URI, {useMongoClient: true});
  
  /*
  Also doesn't work
  mongoose.connect(process.env.MONGODB_URI, {
      "auth":{"authSource": "admin"},
      "user": "admin",
      "pass": "password"
  });
  */
  
  // Should allow us to use promises with mongoose
  mongoose.Promise = global.Promise;
  
  
  var db = mongoose.connection;
  db.on('error', console.error.bind(console, 'MongoDB connection error:'));
  
  
  // Sets up a Schema for the DB
  var urlSchema = new mongoose.Schema({
    url: String,
    shortenedUrl: String
  });
  
  
  // Sets up a model for the DB
  var Url = mongoose.model("Url", urlSchema);
  
  
  app.use(cors());
  
  /** this project needs to parse POST bodies **/
  // you should mount the body-parser here
  var urlencodedParser = bodyParser.urlencoded({ extended: false });
  app.use(bodyParser.json()); // support json encoded bodies
  app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
  
  app.use('/public', express.static(process.cwd() + '/public'));
  
  // parse application/x-www-form-urlencoded
  
  
  app.get('/', function(req, res){
    res.sendFile(process.cwd() + '/views/index.html');
  });
  
  
  app.get("/api/shorturl/new", function (req, res) {
    res.sendFile(process.cwd() + '/views/form.html');
    //res.send({hi: "hi"});
  });
  
  
  app.post("/api/shorturl/new", urlencodedParser, function (req,res) {
    // Gets URL from form
    //var url = req.body.url;
    //console.log(url);
  
    var myData = new Url(req.body);
  
    console.log("myData : " + myData);
  
    myData.save()
      .then(item =&gt; {
        res.send("Successfully saved to DB");
      })
      .catch(err =&gt; {
        res.status(400).send("Unable to save to DB");
      });
  
  });
  
  app.listen(port, function () {
    console.log('Node.js listening ...');
  });
  </code></pre>