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

pre middleware .save conflict with strict mode #14399

Closed
1 task done
SaadMu7ammad opened this issue Mar 1, 2024 · 7 comments
Closed
1 task done

pre middleware .save conflict with strict mode #14399

SaadMu7ammad opened this issue Mar 1, 2024 · 7 comments
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale

Comments

@SaadMu7ammad
Copy link

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.1.3

Node.js version

20.10.0

MongoDB version

6.0.8

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

22H2 - windows 11

Issue

when writing a pre middleware to encrypt the password while saving it shows me an error
image

No overload matches this call.
The last overload gave the following error.
Argument of type '"save"' is not assignable to parameter of type 'RegExp | "insertMany"'.ts(2769)
index.d.ts(413, 5): The last overload is declared here.

and i dont know why that middleware called

important to know that when removing the strict mode it works well with no issues

steps to reproduce

add "strict": true in the ts.config

then start creating a model for example a user

const userSchema = new Schema({
  userId: { type: String, required: true, unique: true },
    password: { type: String, required: true },

  
  })

setup the interface for user

 export interface IUser {
  userId: string;
  password: string;
}
export interface IUserDocument extends IUser,Document {}

now just try to add the pre middleware for encrypting the password

userSchema.pre("save",async function (this: IUserDocument, next: NextFunction) {
    try {
      if (!this.isModified('password')) {
        //to not change password every time we edit the user data
        console.log('password not changed');
        return next();
      } else {
        const salt = await bcrypt.genSalt(10);
        this.password = await bcrypt.hash(this.password, salt);
        console.log('password has been changed');
        next();
      }
    } catch (err) {
      const error = new Error(
        'problem while handling the password',
        404
      );
      next(error);
    }
  }
);

and that happended i think
to call that

pre<T = THydratedDocumentType>(method: 'save', options: SchemaPreOptions, fn: PreSaveMiddlewareFunction<T>): this;

from the node_moduels

not

pre<T = TModelType>(method: 'insertMany' | RegExp, options: SchemaPreOptions
@SaadMu7ammad SaadMu7ammad added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Mar 1, 2024
@FaizBShah
Copy link
Contributor

I tried to reproduce your code in my system with the same version, and its working fine, even with strict: true. Are you still facing this issue?

@SaadMu7ammad
Copy link
Author

I tried to reproduce your code in my system with the same version, and its working fine, even with strict: true. Are you still facing this issue?

yup , still having an err

@vkarpov15
Copy link
Collaborator

Does this error occur when you build your project with TypeScript, or just in your IDE? If with TypeScript, please provide which version of TypeScript you're using.

@SaadMu7ammad
Copy link
Author

Does this error occur when you build your project with TypeScript, or just in your IDE? If with TypeScript, please provide which version of TypeScript you're using.

with ts compiler i even cant run the code except removing it
image

"typescript": "^5.3.3"

@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary help wanted labels Mar 15, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.2.2, 8.2.3, 8.2.4 Mar 15, 2024
@vkarpov15 vkarpov15 modified the milestones: 8.2.4, 8.2.5 Mar 28, 2024
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Apr 8, 2024
@vkarpov15
Copy link
Collaborator

Does this issue only appear in your IDE, or when you try to compile? Also, what is NextFunction?

The following code (gh-14399.ts) compiles fine with TypeScript 5.3.3 and Mongoose 8.1.3:

import { Schema } from 'mongoose';

const userSchema = new Schema({
  userId: { type: String, required: true, unique: true },
    password: { type: String, required: true },


});

 export interface IUser {
  userId: string;
  password: string;
}
export interface IUserDocument extends IUser,Document {}

userSchema.pre("save",async function (this: IUserDocument, next: Function) {
  }
);

Command and output:

$ ./node_modules/.bin/tsc --strict gh-14399.ts 
$

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue typescript Types or Types-test related issue / Pull Request labels Apr 8, 2024
@vkarpov15 vkarpov15 removed this from the 8.3.1 milestone Apr 8, 2024
Copy link

This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale label Apr 23, 2024
Copy link

This issue was closed because it has been inactive for 19 days and has been marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. Stale
Projects
None yet
Development

No branches or pull requests

3 participants