Closed
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.14.1
Node.js version
20.10.0
MongoDB server version
7.0
Typescript version (if applicable)
5.8.3
Description
After upgrading from Mongoose 8.13.2 to 8.14.1, my post('init') hook no longer type-checks in TypeScript.
Argument of type '"init"' is not assignable to parameter of type …
This appears to be caused by a change to the MongooseDistinctDocumentMiddleware signature in this commit.
Steps to Reproduce
Using the post('init') and updating from 8.13.2 to 8.14.1.
NOTE: I am also using NestJS (v. 10.4.17) with the NestJS mongoose library (v. 10.1.0).
import { DateTime } from 'luxon';
import { SchemaFactory } from '@nestjs/mongoose';
import { CallbackWithoutResultAndOptionalError, Document } from 'mongoose';
export const ScheduleEntrySchema = SchemaFactory
.createForClass(ScheduleEntry)
// <-- TypeScript error here after upgrading to 8.14.1
.post('init' as any, function (this: Document, res: any, next: CallbackWithoutResultAndOptionalError) {
try {
// Convert JS Date to Luxon DateTime
this.set('startDate', DateTime.fromJSDate(this.get('startDate')));
if (this.get('endDate')) {
this.set('endDate', DateTime.fromJSDate(this.get('endDate')));
}
} catch (err) {
console.error(err);
}
next();
})
.set('toObject', {
transform: (_doc, ret) => {
// TODO: Refactor later as its pretty hacky and doesn't follow the excepted behavior of toObject
if (_doc.get('startDate') instanceof DateTime) {
ret.startDate = _doc.get('startDate');
} else if (_doc.get('startDate')) {
ret.startDate = DateTime.fromJSDate(_doc.get('startDate'), { zone: 'utc' });
}
if (_doc.get('endDate') instanceof DateTime) {
ret.endDate = _doc.get('endDate');
} else if (_doc.get('endDate')) {
ret.endDate = DateTime.fromJSDate(_doc.get('endDate'), { zone: 'utc' });
}
return ret;
}
});
Expected Behavior
The post('init') hook should accept "init" without requiring a cast, and should behave exactly as it did in 8.13.2.
Metadata
Metadata
Assignees
Labels
No labels