Search code examples
mongodbmeteorsimple-schema

Error trying to update document using autoForm with Schema in Meteor


Error message:

"Uncaught Error: After filtering out keys not in the schema, your modifier is now empty"

Using autoform with collection2 and simple schema in Meteor. The schema:

Injuries = new Mongo.Collection('injuries');

Rehab = new SimpleSchema({
  exercise: {
    type: String,
    label: "Rehab Exercise"
  },
  sets: {
    type: Number,
    label: "Sets"
  },
  duration: {
    type: Number,
    label: "Set Duration (in Minutes)"
  },
  date: {
    type: String,
    label: "Date of Rehab Exercise"
  },
  rehabnotes: {
    type: String,
    label: "Notes: i.e. 70% Intensity During Sprints",
    max: 200
  },
  injuryid:{
    type: String,
  }
});

Injuries.attachSchema(new SimpleSchema({
  player: {
    type: String,
    label: "Player",
    max: 50
  },
  injury: {
    type: String,
    label: "Injury"
  },
  notes: {
    type: String,
    label: "Notes",
    max: 200
  },
  injurydate: {
    type: Date,
    label: "Date of Injury",
  },
  rehab: {
    type: [Rehab],
    optional: true
  }
}));

And the Form Code in the Template:

 {{#autoForm collection="Injuries" schema="Rehab" id="insertRehabForm" type="update"}}
          <fieldset>

                {{> afQuickField name='exercise' options=options}}
                {{> afQuickField name='sets'}}
                {{> afQuickField name='duration'}}
                {{> afQuickField name='date'}}
                {{> afQuickField name='rehabnotes' rows=6}}

          </fieldset>
           <button type="submit" class="btn btn-primary">Insert</button>
                {{/autoForm}}

I can insert documents just fine with the autoform on the home page, using this custom form on the individual document page I receive the error on submission.

I have one collection hook setup up for before submissions, but this looks like it is just a schema error, perhaps the Rehab array that I have set up on the original Injuries schema is messing this up? The searches I've done for this have all been about the "Type" parameter in the schema not matching what is expected, but I've checked those here and they look good. Suggestions?


Solution

  • Based on the AutoForm's docs: the schema attribute is required if collection attribute is not set, however, even if collection is set AutoForm will still use the provided schema attribute to generate (only applicable to QuickForm) and validate the form (applicable to both AutoForm and QuickForm).

    What happened in your case is that since both attributes (schema and collection) are provided, AutoForm first validates the form fields against the Rehab schema and when it succeed, it tries to insert the values of those fields (exercise, sets, duration, date, rehabnotes) to your Injuries collection, which does not have those keys in its own schema (it only has player, injury, notes, injurydate and rehab).

    From your requirements, it seems like setting the AutoForm type to update-pushArray is the best possible solution. Check the docs and the example for usage.