Mongoose 实例方法无法从文档打字稿访问属性

时间:2021-02-24 11:10:22

标签: typescript mongoose mongoose-schema

我的模型界面

export interface IUser extends Document {
    name: string;
    email: string;
    photo: string;
    password: string;
    passwordConfirm: string | undefined;
    correctPassword: (candidatePassword: string, userPassword: string) => boolean;
    changedPasswordAfter: (timestamp: string) => Date;
}

我的模型 -

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

我的实例方法-

userSchema.methods.changedPasswordAfter = function (timestamp: string): Date {
    if (this.passwordChangedAt) {
        //
    }

    return false;
};

打字稿显示的错误 - 类型 Document<any> 上不存在属性“passwordChangedAt”

这是为什么?我该如何解决这个问题?

0 个答案:

没有答案