0

mongoose: ^6.6.2
@nestjs/mongoose: ^9.2.0

I've got the following model :

export class FormNotificationsLayout {
  @Prop()
  public logo?: string;
}

export class FormNotifications {
  @Prop()
  public layout?: FormNotificationsLayout;
}

@Schema({ toJSON: { virtuals: true }, toObject: { virtuals: true } })
export class Form {
  @Prop({ required: true })
  public notifications: FormNotifications;
}

I'd like form.notifications.layout.logo to be stored as an uuid and to populate it with a File from another collection when fetching a Form

I don't want to use the File's mongo _id so I figured I can't do a 'simple' populate by ref

I'm then trying to populate a virtual like so :

export const FormSchema = SchemaFactory.createForClass(Form).virtual(
  'logoFile',
  {
    ref: 'File',
    foreignField: 'uuid',
    localField: 'notifications.layout.logo',
  },
);

then I ideally would fetch the entity and populate

const entity = await this._repository
  .findOne({ id })
  .populate('logoFile');

But I can't seem to specify a virtual in the first place because whatever name I pass to it (the 1st parameter, "logoFile" in the example) I get the following error :

TypeError: Invalid schema configuration: `LogoFile` is not a valid type at path `path`. See https://mongoosejs.com/docs/guide.html#definition for a list of valid schema types.

Why the name of the virtual needs to be a valid Schema eludes me.. I've been referring to this doc and it seems the name of the virtual should be a simple string. What Am I missing ?

0 Answers0