我尝试为用户模式创建 hashPassword 方法。
schema.method("hashPassword", function (): void {
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync(this.password, salt);
this.password = hash;
});
Run Code Online (Sandbox Code Playgroud)
Property 'password' does not exist on type 'Document<any>'.并收到密码错误
这是我的文件
import mongoose, { Schema, Document } from "mongoose";
import bcrypt from "bcryptjs";
/**
* This interface should be the same as JWTPayload declared in types/global.d.ts file
*/
export interface IUser extends Document {
name: string;
email: string;
username: string;
password: string;
confirmed: boolean;
hashPassword: () => void;
checkPassword: (password: …Run Code Online (Sandbox Code Playgroud)