mongoose nestjs MongoDB uniqie字段在函数上不起作用

wlzqhblo  于 2024-01-08  发布在  Go
关注(0)|答案(2)|浏览(206)

需要帮助在nestjs - mongodb模式中为我的名字属性设置uniqe:true,但它不像那样作为默认值工作。

  1. @Schema()
  2. export class User {
  3. @Prop()
  4. userId:string;
  5. @Prop({
  6. type:String,
  7. required:true,
  8. })
  9. name:string;
  10. @Prop({ required: true})
  11. password:string;
  12. @Prop({ required: true })
  13. email:string;
  14. @Prop({default:"user"})
  15. role:string
  16. }

字符串
这是工程必需的或默认的,但不工作的uniqe字段

jjjwad0x

jjjwad0x1#

只需像这样将unique属性添加到名称@Props

  1. @Prop({
  2. type:String,
  3. required:true,
  4. unique: true
  5. })
  6. name: string;

字符串

xa9qqrwz

xa9qqrwz2#

也许你的意思是验证字段,使用IsUnique,类似于:

  1. import { Schema, Prop } from '@nestjs/mongoose';
  2. import { IsUnique } from 'class-validator';
  3. @Schema()
  4. export class User {
  5. @Prop()
  6. userId:string;
  7. @Prop({
  8. type:String,
  9. required:true,
  10. })
  11. name:string;
  12. @Prop({ required: true})
  13. password:string;
  14. @Prop({ required: true })
  15. @IsUnique({ message: 'The field must be unique.' })
  16. email:string;
  17. @Prop({default:"user"})
  18. role:string
  19. }

字符串

展开查看全部

相关问题