Я пытаюсь создать простого пользователя Schema
с Mongoose
в typescript
import mongoose, { Schema, Document } from "mongoose";
import bcrypt from "bcrypt";
const SALT_WORK_FACTOR = 10;
interface IUser extends Document {
username: string;
password: string;
email: string;
activeMembership: () => boolean;
membershipExpires: Date;
registeredOn: Date;
lastLogin: Date;
isAdmin: boolean;
}
const userSchema: Schema<IUser> = new Schema({
username: { type: String, required: true },
password: { type: String, required: true },
email: { type: String, required: true },
activeMembership: { type: Boolean, required: true, default: false },
membershipExpires: { type: Number, required: true, default: 0 },
registeredOn: { type: Date, required: true },
lastLogin: { type: Date },
isAdmin: { type: Boolean, required: true, default: false }
});
userSchema.pre("save", function (next) {
const user = this;
if (!user.isModified("password")) return next();
bcrypt.genSalt(SALT_WORK_FACTOR, (err, salt) => {
if (err) throw err;
bcrypt.hash(user.password, salt, (err, hash) => {
if (err) throw err;
user.password = hash;
next();
});
});
});
Но машинопись продолжает жаловаться:
tsserver says Property 'password' does not exist on type 'Document'.
в строках:
bcrypt.hash(user.password, salt, (err, hash) => {
и:
user.password = hash;
Что я делаю не так?