...
export type UserDocument = HydratedDocument<User>;
@Schema()
export class User extends BaseSchema {
@Transform(({ value }) => value.toString())
@Type(() => String)
_id: ObjectId;
@Prop()
code: string;
@Prop({ required: true })
first_name: string;
@Prop({ required: true })
last_name: string;
@Prop({ required: true, unique: true })
email: string;
@Prop({ required: true })
@Exclude()
password: string;
@Prop({ type: [{ type: Types.ObjectId, ref: Role.name }] })
roles: Role[];
}
export const UserSchema = SchemaFactory.createForClass(User);
...
export type RoleDocument = HydratedDocument<Role>;
@Schema()
export class Role extends BaseSchema {
@Transform(({ value }) => value.toString())
@Type(() => String)
_id: ObjectId;
@Prop({ required: true })
name: string;
@Prop({ required: true, unique: true })
code: string;
}
export const RoleSchema = SchemaFactory.createForClass(Role);
The result:
[
{
"_id": "650857f658bf0a6db907858f",
"created_by": "Hello World",
"code": "USER-cccacd7370038ab879603f5f88d8f1ef",
"first_name": "Hello My",
"last_name": "World",
"email": "hellomyworld@gmail.com",
"roles": [
{
"_id": {},
"created_by": "admin",
"name": "Staffia",
"code": "Staff",
"created_at": "2023-09-17T22:13:41.094Z",
"updated_at": "2023-09-17T22:13:41.094Z",
"__v": 0,
"updated_by": "admin"
}
],
"created_at": "2023-09-18T14:00:22.567Z",
"updated_at": "2023-09-18T15:00:13.902Z",
"__v": 0
}
]
As you can see, the "_id" value of role is not showing correctly. How can I solve this?