#ObjectId is not showing correctly in the searching result.

7 messages · Page 1 of 1 (latest)

raw olive
#
...
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?

alpine river
untold stone
#

can you show the code for creating the role document

raw olive
# untold stone can you show the code for creating the role document

This is the role.schema.ts.

import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { HydratedDocument, ObjectId } from 'mongoose';
import { Type, Transform } from 'class-transformer';
import { BaseSchema } from './base.schema';

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);
raw olive
# untold stone can you show the code for creating the role document

This is the repository.ts

...
async find(
    entityFilter: FilterQuery<T>,
    projection?: Record<string, unknown>,
    options?: Record<string, unknown>,
    sort?: Record<string, SortOrder>,
    populateOptions?: PopulateOptions | PopulateOptions[],
  ): Promise<T[]> {
    const query = this.entityModel.find(
      entityFilter,
      { ...projection },
      options,
    );

    if (sort) query.sort(sort);
    if (populateOptions) query.populate(populateOptions);
    return query.exec();
  }
...
raw olive
# untold stone can you show the code for creating the role document

This is the service.ts.

...
async getUsers(params: Record<string, unknown> = {}): Promise<User[]> {
    try {
      const users = await this.userRepository.find(params, 'roles');
      if (!users) {
        throw new NotFoundException('User not found');
      }
      return users;
    } catch (error) {
      if (error instanceof NotFoundException) {
        throw error;
      }
      throw new Error('An error occurred while retrieving users');
    }
  }
...
untold stone
#

i meant where do you create the role