Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@vkarpov15 skipMiddlewareFunction only works for Query not QueryCursor, here is an example #13411

Closed
vkarpov15 opened this issue May 17, 2023 · 1 comment · Fixed by #13436
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@vkarpov15
Copy link
Collaborator

          @vkarpov15 `skipMiddlewareFunction` only works for `Query` not `QueryCursor`, here is an example
// setup a pre hook for find
schema.pre('find', function (next) {
  next(skipMiddlewareFunction([]));
});
// err is { "args": [[]] }, an instance of skipWrappedFunction
mongooseModel.find().cursor().on('error', function (err) { console.log(err); });

The reason is in lib/cursor/QueryCursor.js, it called like this

// here may want to call query._find()
// or wrap this with kareem.createWrapper()
model.hooks.execPre('find', query, (err) => {
  if (err != null) {
    _this._markError(err);
    _this.listeners('error').length > 0 && _this.emit('error', err);
    return;
  }
  // do other things
});

Originally posted by @junmiu in #11927 (comment)

@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label May 17, 2023
@vkarpov15 vkarpov15 added this to the 7.1.3 milestone May 17, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels May 24, 2023
@IslandRhythms
Copy link
Collaborator

IslandRhythms commented May 24, 2023

image

confirmed on 7.2

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  age: Number
});

testSchema.pre('find', function(next) {
  next(mongoose.skipMiddlewareFunction([]));
});


const Test = mongoose.model('Test', testSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  for (let i = 0; i < 11; i++) {
    await Test.create({ age: i });
  }
  Test.find().cursor().on('error', function(err) { console.log ('the err', err )});
  console.log('done');
}

run();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
2 participants