Skip to content

Not working with NestJS #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
xinsodev opened this issue Nov 19, 2022 · 0 comments
Open

Not working with NestJS #4

xinsodev opened this issue Nov 19, 2022 · 0 comments

Comments

@xinsodev
Copy link

xinsodev commented Nov 19, 2022

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>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant