dsanel / mongoose-delete

Mongoose Soft Delete Plugin
MIT License
346 stars 101 forks source link

mongoose 6 with 'overrideMethods: true' got error #115

Closed liuxyCN closed 1 year ago

liuxyCN commented 2 years ago

mongoose 6 with 'overrideMethods: true' got error, pls help.

nodejs.MongooseError: Model.findOne() cannot run without a model as this. Make sure you are not calling new Model.findOne()

freitasjuciel commented 2 years ago

I had the same issue. Updating mongoose to the lastest available version fixed the issue

masioka commented 2 years ago

I am getting the same issue when using mongoose version 6.4.3, any help on this pls.

wendellpalazzo commented 2 years ago

me too

wendellpalazzo commented 2 years ago

Hey, guys!!

I saw this new implementation in mongoose 6+

in /lib/model.js:

function _checkContext(ctx, fnName) {
  // Check context, because it is easy to mistakenly type
  // `new Model.discriminator()` and get an incomprehensible error
  if (ctx == null || ctx === global) {
    throw new MongooseError('`Model.' + fnName + '()` cannot run without a ' +
      'model as `this`. Make sure you are calling `MyModel.' + fnName + '()` ' +
      'where `MyModel` is a Mongoose model.');
  } else if (ctx[modelSymbol] == null) {
    throw new MongooseError('`Model.' + fnName + '()` cannot run without a ' +
      'model as `this`. Make sure you are not calling ' +
      '`new Model.' + fnName + '()`');
  }
}

my suspicions are in this _checkContext validator

modelSymbol in this context is "mongoose#Model" but its not finding on ctx

dsanel commented 1 year ago

The new version (1.0.0) is heavily tested on Mongoose 5, 6, and 7. I didn't notice this problem.