3

When updating to Mongoose 6 with Mongodb nodejs driver version 4, I have the error below:

MongoInvalidArgumentError: No AuthProvider for DEFAULT defined.
    at prepareHandshakeDocument (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:153:29)
    at performInitialHandshake (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:63:5)
    at /home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:25:9
    at callback (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:244:9)
    at Socket.connectHandler (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:282:9)
    at Object.onceWrapper (events.js:519:28)
    at Socket.emit (events.js:400:28)
    at Socket.emit (domain.js:470:12)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:10)
From previous event:
    at NativeConnection.Connection.openUri (/home/arch/git/project/node_modules/mongoose/lib/connection.js:778:19)
    at /home/arch/git/project/node_modules/mongoose/lib/index.js:330:10
    at promiseOrCallback (/home/arch/git/project/node_modules/mongoose/lib/helpers/promiseOrCallback.js:10:12)
    at Mongoose._promiseOrCallback (/home/arch/git/project/node_modules/mongoose/lib/index.js:1151:10)
    at Mongoose.connect (/home/arch/git/project/node_modules/mongoose/lib/index.js:329:20)
    at connectToDb (/home/arch/git/project/core/configs/database.js:101:8)
    at createServer (/home/arch/git/project/app.js:65:16)
    at Object.<anonymous> (/home/arch/git/project/app.js:262:3)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

I don't have any authentication set up (this is my local development environment) so I don't understand where is the issue comming from. Here is the relevant connection code:

let connectionURL = 'mongodb://';
const connectionOptions = {
  authSource: options.authSource,
};

if (options.user && options.password) {
  connectionURL += `${options.user}:${options.password}@`;
}

connectionURL += `${options.host}/${dbName}?authSource=${options.authSource}`;

mongoose
  .connect(connectionURL, connectionOptions, function onConnected(err) {
    if (err) {
      return cb(new Error(`Can not connect to database ${dbName}`));
    }

    return cb(null, mongoose.connection);
  });
Antoine
  • 3,880
  • 2
  • 26
  • 44

1 Answers1

2

For a passwordless/userless connection, making sure the connection url doesn't contain authSource worked for me. This worked somehow before the version 4 of the driver, but not any more.

Also, either choose wether to pass options in the object or in the connection string. As the doc says:

Best practice is to put options that likely differ between development and production, like replicaSet or ssl, in the connection string, and options that should remain constant, like connectTimeoutMS or poolSize, in the options object

const connectionOptions = {};

if (options.user && options.password) {
  connectionOptions.user = options.user;
  connectionOptions.password = options.password;
  connectionOptions.authSource = options.authSource;
}

const connectionURL = `mongodb://${options.host}/${dbName}`;

mongoose
  .connect(connectionURL, connectionOptions, function onConnected(err) {
    if (err) {
      return cb(new Error(`Can not connect to database ${dbName}`));
    }

    return cb(null, mongoose.connection);
  });
};
Antoine
  • 3,880
  • 2
  • 26
  • 44