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

Query middleware hook is being fired by "init" #9316

Closed
jayadrathamondal opened this issue Aug 11, 2020 · 0 comments
Closed

Query middleware hook is being fired by "init" #9316

jayadrathamondal opened this issue Aug 11, 2020 · 0 comments
Milestone

Comments

@jayadrathamondal
Copy link

jayadrathamondal commented Aug 11, 2020

Do you want to request a feature or report a bug?
bug

What is the current behavior?
Middleware "init" is a document middleware. But fires for the following hook where I have specifically added document: false.

const postHook = function (error, docs, next) {
    console.log(`postHook -> error, docs, next`, error, docs, next)
    if (error) {
        // Transform the error
    }
    next(error);
};

schema.post(/^\w+/, { query: true, document: false }, postHook);

If the current behavior is a bug, please provide the steps to reproduce.

Use the above hook for any schema and then do any query like following.

const emp = await EmployeeModel.create(EmpData);
await EmployeeModel.findById(emp.id);

This will give error TypeError: next is not a function
And see there is a console log in the postHook which gives following.

postHook -> error, docs, next {
      _id: 5f32efb10c0e5b5ad79d601d,
      name: 'Full Name',
      username: 'Emp_1',
      bio: 'He is Employee-Emp_1',
      email: 'emp_1@domain.com',
      age: 28,
      countryCode: 'IND',
      createdAt: 2020-08-11T19:21:21.398Z,
      updatedAt: 2020-08-11T19:21:21.398Z,
      __v: 0,
  } undefined undefined

In which above log its clear that the next is undefined & error is the document itself. This should not be the case for an error handler hook.
As per doc init hooks are synchronous and doesn't receive next as a parameter.
So I thought may be after findById the hook is being fired. So I changed the regexp where I'm adding the postHook in the schema, with the following.

schema.post(/^!i\w+$/, { query: true, document: false }, postHook);

This just says, for all which not starts with "i". And now the error is gone. So I'm thinking that for the init middleware the hook is being fired.

What is the expected behavior?
Middleware "init" is a document middleware. Should not fire when I'm adding hook for only query middleware..

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
nodejs@14.4.0
mongoose@5.9.28
mongodb@4.2.6

@jayadrathamondal jayadrathamondal changed the title init is a document middleware but being fired for the query middleware too. init is a document middleware but being fired for the query middleware hook too. Aug 11, 2020
@jayadrathamondal jayadrathamondal changed the title init is a document middleware but being fired for the query middleware hook too. Query middleware hook is being fired by "init" Aug 11, 2020
@vkarpov15 vkarpov15 added this to the 5.10.1 milestone Aug 16, 2020
vkarpov15 added a commit that referenced this issue Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants