代码之家  ›  专栏  ›  技术社区  ›  nerrood

Swagger没有显示mongoose模式中的_id

  •  0
  • nerrood  · 技术社区  · 3 年前

    我试图在我的项目中使用swagger来获得更好的api请求帮助。一切都很好,但我在展示方面有问题 _id 属性。

    我不知道该怎么连接 _身份证 在我的文档模型中,让swagger看到它。

    例如,我有这样的用户文档

    export class User extends Document {
      @ApiProperty({ description: 'The username of the user' })
      @Prop({ type: String, required: true })
      username: string;
    
      @ApiProperty({ description: 'The email of the user' })
      @Prop({ required: true })
      email: string;
    
      @ApiProperty({ description: 'The hashed password of the user' })
      @Prop({ required: true, min: 6 })
      password: string;
    
      @ApiProperty({ description: 'User roles' })
      @Prop({
        type: [{ type: String, enum: UserRole }],
        default: [UserRole.Customer],
      })
      role: string[];
    
      @ApiProperty({ description: 'User billing details' })
      @Prop({ type: BillingSchema, default: new Billing() })
      billing: Billing;
    
      @ApiProperty({ description: 'Information about user account status' })
      @Prop({ type: Boolean, default: false })
      isActive: boolean;
    
      @ApiProperty({ description: 'User activation token used to account activation' })
      @Prop({ type: String || null, default: null })
      activationToken: string | null;
    
      @ApiProperty({ description: 'User reset password token used to reset password' })
      @Prop({ type: String || null, default: null })
      resetPasswordToken: string | null;
    
      @ApiProperty({ description: 'List of user bought products', type: Product, isArray: true })
      @Prop([{ type: mongoose.Schema.Types.ObjectId, ref: 'Product' }])
      products: Product[];
    
      @ApiProperty({ description: 'Date of register' })
      @Prop({ type: Date })
      createdAt: Date;
    
      @ApiProperty({ description: 'Date of account last update' })
      @Prop({ type: Date })
      updatedAt: Date;
    }
    

    swagger中的任何我的用户模式都是这样的:

    User{
    username*   [...]
    email*  [...]
    password*   [...]
    role*   [...]
    billing*    {...}
    isActive*   [...]
    activationToken*    [...]
    resetPasswordToken* [...]
    products*   [...]
    createdAt*  [...]
    updatedAt*  [...]
    }
    

    我将感谢任何提示或帮助解决这个问题。

    0 回复  |  直到 3 年前
        1
  •  0
  •   Malkavianson    3 年前

    我不知道你为什么要把你的ID放在你的输入模式中,但你只需要添加 _身份证 在你的课堂上:

    export class User extends Document {
      @ApiProperty({ description: 'The id of the user' })
      @Prop({ type: String, required: true })
      _id: string;
    
      @ApiProperty({ description: 'The username of the user' })
      @Prop({ type: String, required: true })
      username: string;
    
      @ApiProperty({ description: 'The email of the user' })
      @Prop({ required: true })
      email: string;
    
      @ApiProperty({ description: 'The hashed password of the user' })
      @Prop({ required: true, min: 6 })
      password: string;
    
      @ApiProperty({ description: 'User roles' })
      @Prop({
        type: [{ type: String, enum: UserRole }],
        default: [UserRole.Customer],
      })
      role: string[];
    
      @ApiProperty({ description: 'User billing details' })
      @Prop({ type: BillingSchema, default: new Billing() })
      billing: Billing;
    
      @ApiProperty({ description: 'Information about user account status' })
      @Prop({ type: Boolean, default: false })
      isActive: boolean;
    
      @ApiProperty({ description: 'User activation token used to account activation' })
      @Prop({ type: String || null, default: null })
      activationToken: string | null;
    
      @ApiProperty({ description: 'User reset password token used to reset password' })
      @Prop({ type: String || null, default: null })
      resetPasswordToken: string | null;
    
      @ApiProperty({ description: 'List of user bought products', type: Product, isArray: true })
      @Prop([{ type: mongoose.Schema.Types.ObjectId, ref: 'Product' }])
      products: Product[];
    
      @ApiProperty({ description: 'Date of register' })
      @Prop({ type: Date })
      createdAt: Date;
    
      @ApiProperty({ description: 'Date of account last update' })
      @Prop({ type: Date })
      updatedAt: Date;
    }
    

    你的描述无助于理解你的问题,如果可以的话,请添加更多细节