Typescript mongoose static model method "Property does not exist by type"

I am currently trying to add a static method to my mongoose scheme, but I cannot find a reason why it does not work this way.

My model:

import * as bcrypt from 'bcryptjs';
import { Document, Schema, Model, model } from 'mongoose';

import { IUser } from '../interfaces/IUser';

export interface IUserModel extends IUser, Document {
    comparePassword(password: string): boolean;
}

export const userSchema: Schema = new Schema({
    email: { type: String, index: { unique: true }, required: true },
    name: { type: String, index: { unique: true }, required: true },
    password: { type: String, required: true }
});

userSchema.method('comparePassword', function (password: string): boolean {
    if (bcrypt.compareSync(password, this.password)) return true;
    return false;
});

userSchema.static('hashPassword', (password: string): string => {
    return bcrypt.hashSync(password);
});

export const User: Model<IUserModel> = model<IUserModel>('User', userSchema);

export default User;

IUser:

export interface IUser {
    email: string;
    name: string;
    password: string;
}

If I try to call User.hashPassword(password), I get the following error[ts] Property 'hashPassword' does not exist on type 'Model<IUserModel>'.

I know that I didn’t define a method anywhere, but I really don’t know where I could express it, since I can’t just put the static method in the interface. I hope you can help me find a mistake, thanks in advance!

+22
source share
4 answers

, , . . .comparePassword() .

User.comparePassword(candidate, cb...)

, schema, model. , , - , mongoose/mongo.

:

passport.use(
  new LocalStrategy({
    usernameField: 'email'
  },
    function (email: string, password: string, done: any) {
      User.findOne({ email: email }, function (err: Error, user: IUserModel) {
        if (err) throw err;
        if (!user) return done(null, false, { msg: 'unknown User' });
        user.schema.methods.comparePassword(password, user.password, function (error: Error, isMatch: boolean) {
          if (error) throw error;
          if (!isMatch) return done(null, false, { msg: 'Invalid password' });
          else {
            console.log('it was a match'); // lost my $HÏT when I saw it
            return done(null, user);
          }
        })
      })
    })
);

, findOne({}), , , user.schema.methods.comparePassword

, :

  • Mine - instance, static. , .
  • , comparePassword(). , , this.password
+11

, , , , TS mongoose ( , , ), .


, , , .

IUser file

  1. IUser IUserDocument. .
  2. Document .
  3. Document.

  1. IUser IUserDocument, , .
  2. IUserModel IUser.
  3. , IUser, IUserDocument, Document IUserDocument.
  4. IUserModel Model<IUser>.
  5. IUserModel.
  6. User Model<IUserModel> IUserModel, IUserModel Model<IUser>.
  7. <IUserModel> <IUser, IUserModel>.

:

import * as bcrypt from 'bcryptjs';
import { Document, Schema, Model, model } from 'mongoose';

import { IUserDocument } from '../interfaces/IUserDocument';

export interface IUser extends IUserDocument {
    comparePassword(password: string): boolean; 
}

export interface IUserModel extends Model<IUser> {
    hashPassword(password: string): string;
}

export const userSchema: Schema = new Schema({
    email: { type: String, index: { unique: true }, required: true },
    name: { type: String, index: { unique: true }, required: true },
    password: { type: String, required: true }
});

userSchema.method('comparePassword', function (password: string): boolean {
    if (bcrypt.compareSync(password, this.password)) return true;
    return false;
});

userSchema.static('hashPassword', (password: string): string => {
    return bcrypt.hashSync(password);
});

export const User: IUserModel = model<IUser, IUserModel>('User', userSchema);

export default User;

( ) ../interfaces/IUserDocument :

import { Document } from 'mongoose';

export interface IUserDocument extends Document {
    email: string;
    name: string;
    password: string;
}
+41

:

, Mongo/Mongoose: .

. - , - , .

. , this .

"" , Document, Document.

TypeScript:

  • Document, .method.
  • (), .static.

, , .

, , , User.findOne (, User.hashPassword, ).

- , , this.save , this.comparePassword .

+5

IUser, , . EG

export interface IUser {
    email: string,
    hash: string,
    salt: string,

    setPassword(password: string): void,
    validPassword(password: string): boolean,
    generateJwt(): string
}

typescript will then recognize your methods and stop complaining

0
source

Source: https://habr.com/ru/post/1015256/


All Articles