-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
lack of documentation of middleware callback #14305
Comments
What specifically about the middleware callback is confusing? Our middleware docs show examples of using |
I'll explain it to you by giving examples of situations I found myself in when working on a task related to this. Goal: Situations:
function (doc, next) { } This is expected and documented, but there's no indication of what happens when I pass an async function; it seems to be working, but I still have to call next somehow (which is not bad; I would prefer if I could have the choice not to do see and return a promise).
In this case, I think it would be better if the
I hope this was clear; thank you and apologies for the delayed response. Edit: updated paragraph structure |
Post Hook of save events (a document middleware) fire twice on each save, and in the first instance, when I access the "this" keyword, it is not a proper document instance as this.constructor.modelName is undefined. This is incorrect. The following script shows that post save hooks only execute once, there's only one "post save" printed to the console: 'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
const schema = mongoose.Schema({ name: String });
schema.post('save', function() {
console.log('post save');
});
const TestModel = mongoose.model('Test', schema);
const doc = new TestModel({ name: 'test' });
doc.save().then(() => console.log('done')); This is expected and documented, but there's no indication of what happens when I pass an async function; it seems to be working, but I still have to call next somehow (which is not bad; I would prefer if I could have the choice not to do see and return a promise). That's fair, we don't have an example in our docs of using async functions with post hooks, just with pre hooks. But post hooks work the same way, and you don't need to call 'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
const schema = mongoose.Schema({ name: String });
schema.post('save', async function() {
console.log('Post save start');
const start = Date.now();
await new Promise(resolve => setTimeout(resolve, 1000));
console.log('post save end', Date.now() - start);
});
const TestModel = mongoose.model('Test', schema);
const doc = new TestModel({ name: 'test' });
doc.save().then(() => console.log('done')); In this case, I think it would be better if the this keyword were more strictly typed to understand what type of object I am manipulating (is this an update or insertMany ? ). If there is already a way for me to type the Query strictly (I believe there is) that I do not understand, then it means it's either hard to find in the documentation or undocumented. That's why we have |
Hi @vkarpov15, thanks for responding.
I just ran that script and it adds up, thank you for pointing that out. I'll check the other dependencies I had... I may have made a mistake
I was referring to typescript types, not runtime behavior. Especially the meaning and behavior of generics in mongoose that I have a hard time grasping (I don't know if this is my fault or the docs tbh). |
Yeah I would recommend defining a hook for each operation, or at least a hook for each general class of operations like However, Which generics are you specifically confused about? With automatic schema type inference, you frequently don't need generics at all. |
Add documentation for calling `schema.post()` with async function
Prerequisites
Issue
Middleware callback functions can have varying parameters; sometimes, pre-hooks have different structures than post-hooks, and query middleware is different than document middleware Yet the structure of the callback lacks clear documentation. I humbly believe it needs to be more evident for newcomers and documented.
The only way to notice this is by inspecting the typescript files on my project, which is slow and time-consuming.
I am ready to contribute to making this improvement in the documentation.
The text was updated successfully, but these errors were encountered: