0
votes

I have a simple schema defined for collection:

testReport = new SimpleSchema({
  name : {
    type: String,
    label: "Your Name"   

  },
  school : {
    type: Object,
    label: "Your School"
  },
  "school.$.name" : {
    type: String
  },
  "school.$.region" : {
    type: String
  }

}, { tracker: Tracker });

Reports.attachSchema(testReport);

This schema is used via autoform to generate very simple form that is saved to Reports collection:

   {{#autoForm collection=Collections.Reports id="form" type="insert"}}
        {{> afQuickField name="name" }}
        {{> afQuickField name="school" options=schoolOptions}}
        <input type="submit" value="Submit">
   {{/autoForm}}

A simple helper is used to autopopulate options dropdown with data from collection:

schoolOptions: function () {
    if (Meteor.userId()) {
      Meteor.user().profile.schools)
            return Meteor.user().profile.schools.map(function (c) {
                return {label: c.name, value: JSON.stringify({ name: c.name, region: c.region})};
      });
    }
  }

Everything is rendering successfully but when I click submit I get an error on School field that says

"Your School must be of type Object"

I did try couple of refactoring but nothing works. Usually in the "value" field of options dropdown we put only a String, Number etc. But I want to pass an object here (which is returned by helper) The object looks like.

{"name":"someSchoolName","region":"someRegion"}

So the final document inserted into Collection should look like:

{  
  "name": "John",
  "school": {"name":"someSchoolName","region":"someRegion"}
}

Can anyone please help with this? Many thanks in advance!

1
In your schema, trying changing this "school.$.name" : { type: String},"school.$.region" : {type: String} to "school.name" : {type:String},"school.region" : {type: String}Abk
@abk that's correct answer. Thanks for your effort! Please put this as an answer not a comment so I can mark it.user3129410

1 Answers

0
votes

In your schema, change this

"school.$.name" : { type: String},"school.$.region" : {type: String}

to "school.name" : {type:String},"school.region" : {type: String}