'Property 'passwordChangedAt' does not exist on type 'Document<User, any, any> & { _id: User; }'

I just started working with Nest.js. However I've faced with an issue in which my mongoose pre save hook in the User schema. Property 'passwordChangedAt' does not exist on type 'Document<User, any, any> & { _id: User; }' Can I have a solution for that? Thanks for your time.

users.module.ts

import { Module } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose';

import { UsersController } from './controllers/users.controller';
import { UsersService } from './users.service';
import { AuthService } from './auth/auth.service';
import { User, UserSchema } from './schemas/users.schema';

@Module({
  imports: [
    MongooseModule.forFeatureAsync([
      {
        name: User.name,
        useFactory: () => {
          const schema = UserSchema;
          
          schema.pre('save', function (next) {
            if (!this.isModified('password') || this.isNew) {
              return next();
            }
            this.passwordChangedAt = Date.now() - 1000;
            next();
          });
          return schema;
        },
      },
    ]),
  ],
  controllers: [UsersController],
  providers: [UsersService, AuthService],
})
export class UsersModule {}

users.chema.ts

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';

export type UserDocument = User & Document;

@Schema()
export class User {
  @Prop({ required: true })
  name: string;

  @Prop({ required: true, unique: true })
  email: string;

  @Prop({ required: true })
  password: string;

  @Prop({ type: Date })
  passwordChangedAt: Date;
}

export const UserSchema = SchemaFactory.createForClass(User);


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source