Skip to content

Commit c44e1d4

Browse files
authored
Fix: rm anonymous functions in .pre hooks
In official mongoose docs everywhere used `function()` instead of `() => {}` https://mongoosejs.com/docs/middleware.html#pre It simply confused me when I've copied code from example and tryed to reach `this` in `.pre('save', () => console.log(this))` there were module itself bcs it loses context. I've spend couple of hours to figure out the issue - it's not that obvious. So my purpose to put `function() {}` there, bcs almost in all cases users will try to do something with document passed to the hook.
1 parent b6b10d0 commit c44e1d4

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

content/techniques/mongo.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ Middleware (also called pre and post hooks) are functions which are passed contr
265265
name: Cat.name,
266266
useFactory: () => {
267267
const schema = CatsSchema;
268-
schema.pre('save', () => console.log('Hello from pre save'));
268+
schema.pre('save', function() { console.log('Hello from pre save') });
269269
return schema;
270270
},
271271
},
@@ -286,11 +286,11 @@ Like other [factory providers](https://docs.nestjs.com/fundamentals/custom-provi
286286
imports: [ConfigModule],
287287
useFactory: (configService: ConfigService) => {
288288
const schema = CatsSchema;
289-
schema.pre('save', () =>
289+
schema.pre('save', function() {
290290
console.log(
291291
`${configService.get('APP_NAME')}: Hello from pre save`,
292292
),
293-
);
293+
});
294294
return schema;
295295
},
296296
inject: [ConfigService],

0 commit comments

Comments
 (0)