Giter Club home page Giter Club logo

soft-delete-mongoose-plugin's People

Contributors

go-die avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

soft-delete-mongoose-plugin's Issues

Not working with NestJS

user.schema.ts

import { Schema, SchemaFactory, Prop } from '@nestjs/mongoose'
import { HydratedDocument } from 'mongoose'
import { Timestamp } from 'typeorm'
import { SoftDelete, SoftDeleteModel } from 'soft-delete-mongoose-plugin'
import {
  IS_DELETED_FIELD,
  DELETED_AT_FIELD,
} from '@/constants/mongoose.constant'
import { SoftDeleteSchema } from './soft-delete.schema'

export type UserDocument = HydratedDocument<User>

@Schema({
  timestamps: {
    createdAt: 'created_at',
    updatedAt: 'updated_at',
  },
  toJSON: {
    transform(doc, ret, options) {
      delete ret.password
      delete ret._id
    },
    versionKey: false,
    virtuals: true,
  },
})
export class User extends SoftDeleteSchema {
  @Prop({ default: null })
  name: string

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

  @Prop({ default: true })
  email_verified: boolean

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

  @Prop()
  phone_national: string

  @Prop()
  region_code: string

  @Prop()
  country_code: number

  @Prop({ default: true })
  phone_verified: boolean

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

  @Prop({ default: null })
  avatar: string

  @Prop({ type: 'number' })
  created_at: Timestamp

  @Prop({ type: 'number' })
  updated_at: Timestamp
}

const UserSchema = SchemaFactory.createForClass(User)

UserSchema.index({
  phone: 'text',
  email: 'text',
  phone_national: 'text',
  name: 'text',
})

UserSchema.plugin(
  new SoftDelete({
    isDeletedField: IS_DELETED_FIELD,
    deletedAtField: DELETED_AT_FIELD,
    mongoDBVersion: '6.0.2',
  }).getPlugin(),
)

export { UserSchema }

I got this exeption when fetching users data

MongooseError: `Model.findOne()` cannot run without a model as `this`. Make sure you are not calling `new Model.findOne()`
    at _checkContext (D:\workspace\hubs\user-repo\node_modules\soft-delete-mongoose-plugin\node_modules\mongoose\lib\model.js:1293:11)
    at Function.findOne (D:\workspace\hubs\user-repo\node_modules\soft-delete-mongoose-plugin\node_modules\mongoose\lib\model.js:2334:3)
    at Function.schema.statics.<computed> [as findOne] (D:\workspace\hubs\user-repo\node_modules\soft-delete-mongoose-plugin\build\index.js:174:63)
    at UsersService.getByUsername (D:\workspace\hubs\user-repo\src\app\users\users.service.ts:21:8)
    at AuthService.validateUser (D:\workspace\hubs\user-repo\src\app\auth\auth.service.ts:26:42)
    at LocalStrategy.validate (D:\workspace\hubs\user-repo\src\app\auth\local.strategy.ts:16:41)
    at LocalStrategy.<anonymous> (D:\workspace\hubs\user-repo\node_modules\@nestjs\passport\dist\passport\passport.strategy.js:20:55)
    at Generator.next (<anonymous>)
    at D:\workspace\hubs\user-repo\node_modules\@nestjs\passport\dist\passport\passport.strategy.js:8:71
    at new Promise (<anonymous>)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.