Thomas Joseph
Thomas Joseph

Reputation: 171

MongooseError: You can not `mongoose.connect()` multiple times while connected

I am getting the below error when trying to connect using moongoose.

MongooseError: You can not mongoose.connect() multiple times while connected.

throw new _mongoose.Error('You can not mongoose.connect() multiple times while connected.'); ^ MongooseError: You can not mongoose.connect() multiple times while connected. at new MongooseError (/node_modules/mongoose/lib/error/mongooseError.js:10:11)

Please help me find the cause for this and how to prevent it

Upvotes: 16

Views: 13740

Answers (4)

H. Almidan
H. Almidan

Reputation: 518

As pointed out by 'iamdimitar', the ability to call mongoose.connect() more than once was removed in this PR. This is said to be done to prevent errors.

If you must call mongoose.connect() more than once, you can use one mongoose.connect() and use mongoose.createConnection() for the rest. That worked for me (I only used one other mongoose.createConnection())

Upvotes: 2

Amin Ahmed Khan
Amin Ahmed Khan

Reputation: 461

In order to use multiple MongoDB connections use mongoose.createConnection function instead of mongoose.connect.

mongoose.createConnection Will give you a connection object which you can further use in your model file, Cause models are always bound to a single connection

let config = require('../config');
let mongoose = require('mongoose');

exports.connect = function () {
  const db = mongoose.createConnection(config.mongoUrl, {
    reconnectInterval: 5000,
    reconnectTries: 60
    // add more config if you need
  });
  db.on(`error`, console.error.bind(console, `connection error:`));
  db.once(`open`, function () {
    // we`re connected!
    console.log(`MongoDB connected on "  ${config.mongoUrl}`);
  });
};

Upvotes: 4

Saffer
Saffer

Reputation: 162

I've had the same problem and solved pretty easily. All i had to do was to remove any connections in my controllers.

Before: Server.js

const mongoose = require('mongoose');
const connectionString = 'mongodb://localhost:27017/DB';
mongoose.connect(connectionString);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
//Server code...

Controller.js

const mongoose = require('mongoose');
const connectionString = 'mongodb://localhost:27017/DB';
mongoose.connect(connectionString);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
//Controller code...

After: Server.js

const mongoose = require('mongoose');
const connectionString = 'mongodb://localhost:27017/DB';
mongoose.connect(connectionString);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
//Server code...

Controller.js

//Controller code...

Obviously I removed it from all my controller files.

Upvotes: 3

iamdimitar
iamdimitar

Reputation: 174

In mongoose version 5.6.1 the check was added https://github.com/Automattic/mongoose/pull/7905

Revert to an older version for a quick fix.

Upvotes: 3

Related Questions