javascript - Setting up typescript with mongoose model - Stack Overflow

For whatever reason typescript is not accepting my code. In the UserScema.pre method the typescript err

For whatever reason typescript is not accepting my code. In the UserScema.pre method the typescript error says the properties createdAt, and password do not exist on the type Document (this). How can I make the typescript interface apply to this method and return an IUserDocument object?

import {Schema, Model, Document, model} from 'mongoose';
import bcrypt from 'bcrypt-nodejs';

export interface IUserDocument extends Document{
    createdAt: Date,
    username: string,
    displayName: string,
    email: string,
    googleId: string,
    password: string,
    verifyPassword(password:string): boolean
}

let UserSchema: Schema = new Schema({
    createdAt:{
        type:Date,
        default:Date.now   
    },
    username: {
        type:String,
        lowercase:true  
    },
    displayName: String,
    email: {
        type:String,
        lowercase:true
    },
    googleId: String,
    password: String
});

UserSchema.pre('save', function(next){
    var user = this;

    if(!this.createdAt) this.createdAt = Date.now;

    if(user.isModified('password')) {
        bcrypt.genSalt(10, function(err:any, salt:number){
            bcrypt.hash(user.password, salt, null, function(err:any, hash:string){
                if(err) return next(err);
                user.password = hash; 
                next();
            });
        });
    } else{
        return next();
    }
});

UserSchema.methods.verifyPassword = function(password:string){
    return bcryptpareSync(password, this.password);
}

const User = model<IUserDocument>('User', UserSchema);
export default User;

My code is derived from this source /.

For whatever reason typescript is not accepting my code. In the UserScema.pre method the typescript error says the properties createdAt, and password do not exist on the type Document (this). How can I make the typescript interface apply to this method and return an IUserDocument object?

import {Schema, Model, Document, model} from 'mongoose';
import bcrypt from 'bcrypt-nodejs';

export interface IUserDocument extends Document{
    createdAt: Date,
    username: string,
    displayName: string,
    email: string,
    googleId: string,
    password: string,
    verifyPassword(password:string): boolean
}

let UserSchema: Schema = new Schema({
    createdAt:{
        type:Date,
        default:Date.now   
    },
    username: {
        type:String,
        lowercase:true  
    },
    displayName: String,
    email: {
        type:String,
        lowercase:true
    },
    googleId: String,
    password: String
});

UserSchema.pre('save', function(next){
    var user = this;

    if(!this.createdAt) this.createdAt = Date.now;

    if(user.isModified('password')) {
        bcrypt.genSalt(10, function(err:any, salt:number){
            bcrypt.hash(user.password, salt, null, function(err:any, hash:string){
                if(err) return next(err);
                user.password = hash; 
                next();
            });
        });
    } else{
        return next();
    }
});

UserSchema.methods.verifyPassword = function(password:string){
    return bcrypt.pareSync(password, this.password);
}

const User = model<IUserDocument>('User', UserSchema);
export default User;

My code is derived from this source http://brianflove./2016/10/04/typescript-declaring-mongoose-schema-model/.

Share Improve this question asked Apr 28, 2018 at 20:16 Keegan TeetaertKeegan Teetaert 6736 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

pre is generic method which parameter defaults to Document. In case this is not so, it should be:

UserSchema.pre<IUserDocument>('save', function(next){ ... });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745346078a4623549.html

相关推荐

  • javascript - Setting up typescript with mongoose model - Stack Overflow

    For whatever reason typescript is not accepting my code. In the UserScema.pre method the typescript err

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信