mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

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

Open garrows opened this issue 10 years ago • 9 comments

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

garrows avatar Sep 06 '16 05:09 garrows

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

rubentlz avatar Sep 12 '16 11:09 rubentlz

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 avatar Sep 12 '16 11:09 ct5845

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

rubentlz avatar Sep 12 '16 11:09 rubentlz

Thanks for reporting, will fix

vkarpov15 avatar Sep 17 '16 01:09 vkarpov15

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 avatar Oct 14 '16 02:10 vkarpov15

@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\mongoose\node_modules\mongodb\lib\db.js:485:36) at Db.dropDatabase (node_modules\mongoose\node_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) {

Amri91 avatar Oct 18 '16 10:10 Amri91

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.

vkarpov15 avatar Oct 22 '16 00:10 vkarpov15

same error on dropCollection. version: 4.6.6

irakli2692 avatar Jun 07 '17 08:06 irakli2692

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

vkarpov15 avatar Jun 09 '17 21:06 vkarpov15