Currently, I am working on setting up a login API using Mongoose with TypeScript and JSON Web Token. As I am still new to TypeScript, I am facing an issue in the user.model file where I am unable to access any schema property using the "this" method. For example, when I try to access user.methods, it gives me a compile error stating "Property 'tokens' does not exist on type 'Document'.ts(2339)".
export interface IUser extends Document {
name: string;
email: string;
password: string;
tokens: { token: string }[];
encryptPassword(password: string): Promise<string>;
validatePassword(password: string): Promise<boolean>;
}
const userSchema = new Schema({
name: {
type: String,
required: true,
trim: true
},
email: {
type: String,
unique: true,
required: true
},
password: {
type: String,
required: true,
minlength: 5,
trim: true
},
tokens: [{
token: {
type: String,
required: true
}
}]
})
userSchema.methods.generateAuthToken = async function () {
const user = this;
const token = jwt.sign({ _id: user._id.toString() }, "thisismysecretkey");
user.tokens = this.tokens.concat({ token });
}
While working in the userSchema.generateAuthToken section, I am encountering the error "Property 'tokens' does not exist on type 'Document'" when attempting to use user.tokens or this.tokens. If you have any insights on what I might be doing wrong, I would greatly appreciate it. Thank you in advance.