-- userSchema.ts interface
import mongoose, { Schema, Document } from "mongoose";
import moment from "moment";
import bcrypt from "bcrypt";
export interface UserDoc extends Document {
name: {
type: string;
required: boolean;
};
email: {
type: string;
required: boolean;
};
password: {
type: string;
required: boolean;
};
dateJoined: {
type: string;
default: string;
};
}
const userSchema = new Schema({
name: {
type: String,
required: true,
},
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
dateJoined: {
type: String,
default: moment().format("MMMM Do YYYY"),
},
});
I created my user model and the problem I'm having is creating the matchPassword method with uses bcrypt to compare the enteredPassword parameter vs the password from the Database
userSchema.methods.matchPassword = async function (enteredPassword) {
return await bcrypt.compare(enteredPassword, this.password); ***
};
userSchema.pre("save", async function (next) {
if (this.isModified("password")) {
next();
}
const salt = bcrypt.genSalt(10);
*** this.password = await bcrypt.hash(this.password, await salt); ***
});
const User = mongoose.model("User", userSchema);
The error message is as follows:
Property 'password' does not exist on type 'Document<any>'.
and this error is on each instance of this.password highlighted by the ***
I have used this same method before in Javascript so I do not know why It doesn't work on typescript and how can I bind the this.password to the Mongoose Document
Thank you
所有评论(0)