Property "password" does not exists on type Document
You need to add type here with pre-save hook as per the mongoose docs, pre hook is defined as,
/**
* Defines a pre hook for the document.
*/
pre<T extends Document = Document>(
method: "init" | "validate" | "save" | "remove",
fn: HookSyncCallback<T>,
errorCb?: HookErrorCallback
): this;
and if you have an interface like below then,
export interface IUser {
email: string;
password: string;
name: string;
}
Add type with pre-save hook,
userSchema.pre<IUser>("save", function save(next) { ... }
I dont know if this will help as the question is old now, however I tried this
import mongoose, { Document } from 'mongoose';
const User = mongoose.model<IUser & Document>("users", UserSchema);
type IUser = {
username: string
password: string
}
mongoose.model
needs a Document type, and you want to extend that document, therefore Unify the two types
you can also pass the interface type to the schema itself.
import { model, Schema, Document } from 'mongoose';
const userSchema = new mongoose.Schema<IUser>({
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
name: { type: String, required: true }
});
interface IUser extends Document{
email: string;
password: string;
name: string;
}