Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose SubDocument #1543

Open
hanza93 opened this issue Jun 11, 2022 · 5 comments
Open

Mongoose SubDocument #1543

hanza93 opened this issue Jun 11, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@hanza93
Copy link

hanza93 commented Jun 11, 2022

Have you read the Contributing Guidelines?

Yes.

Hey, before the question here is a context:
I'm using mongoose and I have 2 models (JobCard, Part) where JobCard has multiple Parts with different statuses for each Part, so I decided to go with subdocument, and here the problem started.

JobCard schema

@Schema({ timestamps: true })
export class JobCard extends Document {
  // ... some properties here
  @Prop({ type: [JobCardPartModel], default: [] })
  parts: JobCardPart[];
}
export const JobCardModel = SchemaFactory.createForClass(JobCard);

JobCardPart schema

@Schema({ timestamps: true })
export class JobCardPart extends Document {
  @Prop({ type: SchemaTypes.ObjectId, ref: 'Part', required: true })
  part!: Types.ObjectId;
}
export const JobCardPartModel = SchemaFactory.createForClass(JobCardPart);

and the DTOs are as follows

JobCard Dto

@ObjectType('JobCard')
@KeySet(['id'])
export class JobCardDto {
  @IDField(() => ID)
  id!: string;
  // some properties here
  @Field(() => [JobCardPartDto])
  parts: JobCardPartDto[];
}

JobCardPart Dto

@ObjectType('JobCardPart')
@KeySet(['id'])
export class JobCardPartDto {
  @IDField(() => ID)
  id!: string;

  @Field({ nullable: true })
  part: PartDto;
}

Part Dto

@ObjectType('Part')
@KeySet(['id'])
export class PartDto {
  @IDField(() => ID)
  id!: string;

  @Field()
  name: string;

  @Field()
  serialNumber!: string;
}

when I try to run this query

query GetOneJobCard {
  jobCard (id: "62a4592867fe1f9e232b3fbe") {
       id
       parts {
     	 part {
         id
      }
      status
      declinedReason
    }
  }
}

I'm getting this error ID cannot represent value: { type: \"Buffer\", data: [Array] }.

  1. Am I doing it right?
  2. Is there a better way to do it?

Thanks in advance.

@hanza93 hanza93 added the bug Something isn't working label Jun 11, 2022
@hanza93
Copy link
Author

hanza93 commented Jun 13, 2022

@doug-martin sorry but I really stuck on this

@kuznetsov-kd
Copy link

kuznetsov-kd commented Jun 17, 2022

@doug-martin sorry but I really stuck on this

This library doesn't work with mongoose v6. You need to use mongoose v5.

@smolinari
Copy link
Collaborator

Or try the Typegoose version. Also the updated fork (look through the issues). This package has been dead for a while.

Scott

@hanza93
Copy link
Author

hanza93 commented Jun 18, 2022

already using the mongoose v5 @DedWatson :(

@kuznetsov-kd
Copy link

already using the mongoose v5 @DedWatson :(

You made a mistake. Look at this
https://doug-martin.github.io/nestjs-query/docs/persistence/mongoose/relations

@ObjectType('JobCard')
@KeySet(['id'])
@Relation('parts', () => JobCardPartDto, { disableRemove: true })
export class JobCardDto {
  @IDField(() => ID)
  id!: string;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants