simpl-schema auto generate _id for subdocuments

101 Views Asked by At

I have a schema for a subdocument as follows

const childSchema = new SimpleSchema({
  _id: {
    type: String,
    autoValue: () => this.docId,
  },

  name: String,

  members: {
    type: Array,
    optional: true,
  },
  'members.$': String,
})

and i am attaching that as a subdocument schema in another schema

const ParentSchema = new SimpleSchema({
  _id: {
    type: String,
    autoValue: () => this.docId,
  },

  name: {
    type: String,
  },

  children: {
    type: Array,
    optional: true,
  },
  'children.$': childSchema,
});

ParentCollection.attachSchema(ParentSchema);

i am updating the parent document as follows

// name and members prepopulated
const child = {
      name,
      members,
    };

ParentCollection.update(
    { _id: id },
    {
      $push: { children: child },
    }
);

My understanding is _id in childSchema would be auto generated because of

_id: {
    type: String,
    autoValue: () => this.docId,
  },

but i get the following error

Error: ID is required (children.0._id) in parent update
I20210922-09:37:29.819(-7)?     at getErrorObject (packages/aldeed:collection2/collection2.js:506:17)
I20210922-09:37:29.820(-7)?     at doValidate (packages/aldeed:collection2/collection2.js:478:13)
I20210922-09:37:29.820(-7)?     at Collection.Mongo.Collection.<computed> [as update] (packages/aldeed:collection2/collection2.js:199:14)
0

There are 0 best solutions below