Skip to content

Not working with NestJS #4

@xinsodev

Description

@xinsodev

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>)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions