-
-
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
TS error on accessing this.model in middlewares in v6 #11435
Labels
typescript
Types or Types-test related issue / Pull Request
Milestone
Comments
IslandRhythms
added
the
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
label
Feb 22, 2022
import * as mongoose from 'mongoose';
interface Test {
name: string;
}
const Item1Schema = new mongoose.Schema<Test>({ name: String });
const Item2Schema = new mongoose.Schema<Test>({ name: String });
Item2Schema.pre('validate', function preValidate() {
const item1Model = this.model('Item1'); // Getting ts error here
console.log(item1Model);
})
const Item1 = mongoose.model<Test>('Item1', Item1Schema);
const Item2 = mongoose.model<Test>('Item2', Item2Schema); |
@IslandRhythms please check this sandbox link https://codesandbox.io/s/elastic-cdn-onoqfr?file=/schema.ts |
vkarpov15
added
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
and removed
can't reproduce
Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
labels
Mar 2, 2022
It seems like Document is missing the model function in the typings and Schema is not narrowing correctly because TInstanceMethods (third generic typing of Schema) is set to any. @IslandRhythms Please add typescript tag. :) |
here the typings test function gh11435() {
interface Item {
name: string;
}
const ItemSchema = new Schema<Item>({ name: String });
ItemSchema.pre('validate', function preValidate() {
expectType<Function>(this.model);
expectType<Model<any, any, any>>(this.model('Item1'))
})
} |
IslandRhythms
added
typescript
Types or Types-test related issue / Pull Request
and removed
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
labels
Mar 7, 2022
vkarpov15
added a commit
that referenced
this issue
Mar 19, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you want to request a feature or report a bug?
I want to report a bug
What is the current behavior?
Getting ts error while accessing one model in another schema's prevalidate middleware.
If the current behavior is a bug, please provide the steps to reproduce.
I was trying to access model using
this.model(model_name)
but getting typescript errorProperty 'model' does not exist on type 'Item2Document'
(code actually works withts-ignore
). I have another codebase that uses mongoose v5 and this works perfectly in v5.What is the expected behavior?
Code should compile without any errors.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
NodeJS = v14
MongoDB = v4.4.0
Mongoose = v6.2.2
The text was updated successfully, but these errors were encountered: