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

[Docs] Contradiction in docs/middleware.md #12113

Closed
2 tasks done
RobbyCBennett opened this issue Jul 16, 2022 · 5 comments
Closed
2 tasks done

[Docs] Contradiction in docs/middleware.md #12113

RobbyCBennett opened this issue Jul 16, 2022 · 5 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@RobbyCBennett
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

Issue

Here's the page.

I ran into this issue while trying to use pre/post middleware with Model.save().

According to the top of the doc, save middleware is only supported for the Document class.

However, the section Define Middleware Before Compiling Models shows that save middleware is supported for Model.

The code snippets in the section don't work for me, but save should be a basic supported feature. For example, pre middleware to save a user can encrypt a password.

@RobbyCBennett RobbyCBennett changed the title Contradiciton in documentation docs/middleware.md [Docs] Contradiction in docs/middleware.md Jul 16, 2022
@RobbyCBennett
Copy link
Author

Additionally, the unordered lists in Types of Middleware could have titles for clarity. For example:

Types of Middleware

Mongoose has 4 types of middleware: document middleware, model middleware, aggregate middleware, and query middleware. Document middleware is supported for the following document functions. In document middleware functions, this refers to the document.

Document:

  • validate
  • save
  • remove
  • updateOne
  • deleteOne
  • init (note: init hooks are synchronous)

@IslandRhythms IslandRhythms added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Jul 18, 2022
@vkarpov15
Copy link
Collaborator

How exactly does the section you linked to "shows that save middleware is supported for Model"? Document middleware = middleware for documents (which are instances of Models). Model middleware = middleware for static functions on models, like insertMany()

@vkarpov15 vkarpov15 added this to the 6.4.7 milestone Jul 20, 2022
@RobbyCBennett
Copy link
Author

RobbyCBennett commented Jul 21, 2022

I misunderstood the unique terms that mongo uses. The following expression snippet makes a Document, but I thought it was a Model because it is an instance of a Model.

new User({ name: 'test' })

I was confused because "documents represent a one-to-one mapping to documents as stored in MongoDB" so I assumed that a model created, but not yet saved was just a Model instance, but not a Document yet.

@RobbyCBennett
Copy link
Author

So I guess there isn't a contradiction in docs/middleware.md, but perhaps the sentence quoted from docs/documents.md could be updated to clarify that documents not a one-to-one mapping to documents as stored in MongoDB, as they can be created and not saved.

@vkarpov15 vkarpov15 modified the milestones: 6.4.7, 6.4.9 Jul 25, 2022
@vkarpov15
Copy link
Collaborator

That's fair. I think it's worth clarifying a little bit the difference between models and documents in the middleware docs.

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
Projects
None yet
Development

No branches or pull requests

3 participants