nestjs-query
nestjs-query copied to clipboard
Mongoose SubDocument
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] }
.
- Am I doing it right?
- Is there a better way to do it?
Thanks in advance.
@doug-martin sorry but I really stuck on this
@doug-martin sorry but I really stuck on this
This library doesn't work with mongoose v6. You need to use mongoose v5.
Or try the Typegoose version. Also the updated fork (look through the issues). This package has been dead for a while.
Scott
already using the mongoose v5 @DedWatson :(
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;
}