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

TS error on accessing this.model in middlewares in v6 #11435

Closed
piyushk96 opened this issue Feb 20, 2022 · 4 comments
Closed

TS error on accessing this.model in middlewares in v6 #11435

piyushk96 opened this issue Feb 20, 2022 · 4 comments
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@piyushk96
Copy link

piyushk96 commented Feb 20, 2022

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 error Property 'model' does not exist on type 'Item2Document'(code actually works with ts-ignore). I have another codebase that uses mongoose v5 and this works perfectly in v5.

import mongoose from 'mongoose';
const { Schema } = mongoose;

const Item1Schema = new Schema({ name: String });
const Item2Schema = new Schema({ name: String });

Item2Schema.pre('validate', function preValidate() {
  const item1Model = this.model('Item1');  // Getting ts error here
})

const Item1 = mongoose.model('Item1', Item1Schema);
const Item2 = mongoose.model('Item2', Item2Schema);

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

@IslandRhythms 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
@IslandRhythms
Copy link
Collaborator

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);

@piyushk96
Copy link
Author

@vkarpov15 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
@vkarpov15 vkarpov15 added this to the 6.2.7 milestone Mar 2, 2022
@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 6, 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. :)

@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 6, 2022

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 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 vkarpov15 reopened this Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

4 participants