title: MongoError: there are no users authenticated
body: <p>I'm trying to write a script to add an admin user and a generic user to the MongoDB database using <code>mongodb</code> NodeJS driver - version <strong>3.0.1</strong></p>
  
  <p>I'm able to create the admin user, but not general user for a database. I'm always getting <code>MongoError: there are no users authenticated</code>. Going through the documentation, the only way to authenticate a user is via URL. I've deleted the database completely from the specified path, tried multiple times, still, I'm stuck.</p>
  
  <p>Here is what I've got so far,</p>
  
  <pre><code>const MongoClient = require('mongodb').MongoClient;
  const format = require('util').format;
  const config = require("./config.json");
  
  var adminUser = process.argv[2],
  adminPassword = process.argv[3],
  url = `mongodb://${config.database.location}:${config.database.port}`,
  authURL = `mongodb://%s:%s@${config.database.location}:${config.database.port}/?authMechanism=SCRAM-SHA-1&amp;authSource=admin`;
  
  if (!adminUser || !adminPassword) {
     throw new Error("Please enter administrator username and password!\nUsage:\tnode init.js &lt;adminUserName&gt; &lt;adminPassword&gt;\n\n");
  }
  
  MongoClient.connect(url, function (err, client) {
      if (err) throw err;
      console.log("Connected successfully to server");
      const db = client.db(config.database.name);
      var adminDb = db.admin();
      adminDb.addUser(adminUser, adminPassword, {
          roles: [{
             role: "userAdminAnyDatabase",
             db: "admin"
          }]
      }).then(function (err, result) {
              MongoClient.connect(format(authURL, encodeURIComponent(adminUser), encodeURIComponent(adminPassword)), function (err, authClient) {
                    if (err) throw err;
                    console.log('Authenticated Successfully');
                    const db = client.db(config.database.name);
                    var adminDb = db.admin();
                    db.addUser(config.database.auth.username, config.database.auth.password, {
                           roles: [{
                               role: "readWrite",
                               db: config.database.name
                           }]
                 }).then(function () {
                     console.log("Setup completed!");
                     authClient.close();
                     client.close();
                 }).catch(function (err) {
                     throw err.stack;
          });
        });
     });
   });
  </code></pre>
  
  <p>Here is my <code>mongod.cfg</code>, the configuration file for <code>mongod</code> process:</p>
  
  <pre><code>systemLog:
        destination: file
        path: D:\MongoDB\logs\mongod.log
  storage:
        dbPath: D:\MongoDB\database
  security:
        authorization: "enabled"
  </code></pre>
  
  <p>And finally the configuration file, <code>config.json</code>:</p>
  
  <pre><code>{
    "database": {
      "location": "localhost",
      "name": "mongodb-test",
      "port": 27017,
      "auth": {
          "username": "testuser",
          "password": "welcome"
       }
     }
   }
  </code></pre>