Typescript mongoose static model method “Property does not exist on type”

491
February 25, 2017, at 04:55 AM

I am currently trying to add a static method to my mongoose schema but I can't find the reason why it doesn't 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;

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

I know that I didn't define the method anywhere but I don't really know where I could put it as I can't just put a static method into an interface. I hope you can help my find the error, thanks in advance!

READ ALSO
Passport.js is not storing/sending passport user info

Passport.js is not storing/sending passport user info

So I am trying to build a simple login/authorization tool utilizing nodejs, passport

437
res.redirect(307, `url`) not sending over data anything on the body to the receiving service

res.redirect(307, `url`) not sending over data anything on the body to the receiving service

Like the title states, I'm having trouble understanding why my resredirect is not redirecting the data originally on its body to its referring service

311
Promise all race conditions with knex migrations

Promise all race conditions with knex migrations

I've been told that there's no guarantee in the order in which a Promiseall resolves its promises

358
Acess values with htmlparser in node.js

Acess values with htmlparser in node.js

i'm new to nodejs and i'm trying to retrieve some values from an html but i don't know how to access those values

328