Mongoose: Error `Cannot read property 'commandsTakeWriteConcern' of null` in v4.6.0

Created on 6 Sep 2016  路  9Comments  路  Source: Automattic/mongoose

On the line
mongoose.connection.db.dropDatabase( done );
We are getting the error

Cannot read property 'commandsTakeWriteConcern' of null
      at decorateWithWriteConcern (/redacted/node_modules/mongoose/node_modules/mongodb/lib/db.js:485:36)
      at Db.dropDatabase (/redacted/node_modules/mongoose/node_modules/mongodb/lib/db.js:872:3)
      at Array.db.dropDatabase (/redacted/src/db/index.js:20:25)
      at node_modules/async/lib/async.js:596:38
      at _arrayEach (node_modules/async/lib/async.js:85:13)
      at Object.async.auto (node_modules/async/lib/async.js:554:9)
      at Context.<anonymous> (test/config.js:25:9)

However the version [email protected] works fine.

Thanks in advance

Most helpful comment

I had the same issue. I got it working by changing where i had my drop statement to;

mongoose.connection.once('connected', () => {
    mongoose.connection.db.dropDatabase();
});

All 9 comments

I am encountering the same problem with [email protected]. Downgraded to 4.5.9 and verifies that it works.

I had the same issue. I got it working by changing where i had my drop statement to;

mongoose.connection.once('connected', () => {
    mongoose.connection.db.dropDatabase();
});

@ct5845 you're right, switched back to 4.6.0 and did it your way, and it works.

Thanks for reporting, will fix

Hmm taking a closer look, the below code hangs on 4.5.9:

'use strict';

Error.stackTraceLimit = Infinity;

var assert = require('assert');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost/gh4490');
mongoose.set('debug', true);

mongoose.connection.db.dropDatabase(error => {
  console.log('Error', error);
  process.exit(0);
});

And 4.6.x gives you the error. I guess the right way is to add a dropDatabase() helper for mongoose, because ussing mongoose.connection.db.dropDatabase() bypasses mongoose entirely.

@vkarpov15 I am still having the same issue even in mongoose 4.6.4. Reverting to 4.5.9 fixes the issue.

I am getting this error:
TypeError: Cannot read property 'commandsTakeWriteConcern' of null
at decorateWithWriteConcern (node_modules\mongoosenode_modules\mongodb\lib\db.js:485:36)
at Db.dropDatabase (node_modules\mongoosenode_modules\mongodb\lib\db.js:874:3)
at Function.module.exports.module.exports.reset (node_modules\mockgoose\Mockgoose.js:159:32)
at Context. (test\integrationTestPushNotifications.js:37:15)

Update:
Using 4.6.4, I was able to make it work by modifying line 485 in mongoose/node_modules/mongodb/lib/db.js
to
if(self.s.topology && self.s.topology.capabilities() && self.s.topology.capabilities().commandsTakeWriteConcern) {

Use this instead:

mongoose.connection.dropDatabase(error => {
  console.log('Error', error);
  process.exit(0);
});

The way we fixed this issue was adding a dropDatabase helper in mongoose that ties in with mongoose's buffering.

same error on dropCollection.
version: 4.6.6

@irakli2692 please open a separate issue and provide a code sample

Was this page helpful?
0 / 5 - 0 ratings