Home > Enterprise >  'MongoError: pool is draining, new operations prohibited' when trying to switch databases
'MongoError: pool is draining, new operations prohibited' when trying to switch databases

Time:10-06

I'm trying to access my prod_clone database and then disconnect that database and then access my development database.

But I getting MongoError: pool is draining, new operations prohibited when I try to grab data from the second database database.

const mongoose = require('mongoose');

const User = require('../models/User.Model');

(async () => {
// first database
  mongoose.connect(process.env.PROD_CLONE_DB_DSN);
  const user = await User.findOne({ email: '[email protected]' });
  mongoose.disconnect();

// 2nd database
  mongoose.connect(process.env.DEVELOPMENT_DB_DSN);
  const user2 = await User.findOne({ email: '[email protected]' });
// ^^^^^^^^^^^^^^ -----------throws MongoError-----------

  mongoose.disconnect();


})();

How can I switch to my second database without throwing this error?

CodePudding user response:

Both connect and disconnect returns a Promise, try to await them:

(async () => {
  try {
    // first database
    await mongoose.connect(process.env.PROD_CLONE_DB_DSN);
    const user = await User.findOne({ email: '[email protected]' });
    await mongoose.disconnect();

    // 2nd database
    await mongoose.connect(process.env.DEVELOPMENT_DB_DSN);
    const user2 = await User.findOne({ email: '[email protected]' });
    // ^^^^^^^^^^^^^^ -----------throws MongoError-----------

    await mongoose.disconnect();
  } catch (err) {
    console.log(err);
  }
})();
  • Related