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

Conflict between doc example and type declaration for query middleware #10531

Closed
mark-night opened this issue Aug 1, 2021 · 3 comments
Closed
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@mark-night
Copy link

Do you want to request a feature or report a bug?
Not sure. Read below.
What is the current behavior?
Doc example reads this.model in query middleware (https://mongoosejs.com/docs/middleware.html#notes),

schema.pre('findOneAndUpdate', async function() {
  const docToUpdate = await this.model.findOne(this.getQuery());
  console.log(docToUpdate); // The document that `findOneAndUpdate()` will modify
});

This does work in practice. However, in typescript, this raises error indicating 'Property "model" not exist in Query...' (with this specified as type Query<any, any> )
The example is somewhat confusing though because query does has findOne method, what is the point reading this.model?

If the current behavior is a bug, please provide the steps to reproduce.
Not sure if this is a spot needs to be updated in doc example or something missed in type declaration.

What is the expected behavior?

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node v12.22.1
Mongoose v5.13.3

@IslandRhythms IslandRhythms added docs This issue is due to a mistake or omission in the mongoosejs.com documentation typescript Types or Types-test related issue / Pull Request labels Aug 2, 2021
@vkarpov15 vkarpov15 added this to the 5.13.6 milestone Aug 3, 2021
@vkarpov15
Copy link
Collaborator

vkarpov15 commented Aug 3, 2021

Query instances have a model property, but looks like that's missing from our TypeScript bindings. Will fix 👍

@vkarpov15 vkarpov15 modified the milestones: 5.13.6, 5.13.7 Aug 9, 2021
@vkarpov15
Copy link
Collaborator

If you do this.findOne() in query middleware, you're switching the current query to findOne, not executing a separate query. You should do this.model.findOne() to create a new query instance.

@mark-night
Copy link
Author

If you do this.findOne() in query middleware, you're switching the current query to findOne, not executing a separate query. You should do this.model.findOne() to create a new query instance.

That's a good hint, never thought that way. It now makes sense clearly! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

3 participants