I need some help with an issue on inserting into a collection. I am using collection2 and simpleschema.
Here is my schema definition.
``Programs = new Mongo.Collection(“programs”);
Exercise = new SimpleSchema({
exerName: {
type: String,
label: “Exercise”,
},
reps: {
type: String,
label: “Reps”,
},
sets: {
type: Number,
label: “Sets”
},
notes: {
type: String,
optional: true,
label: “Notes”
}
});
Group = new SimpleSchema({
grpName: {
type: String,
label: “Group number”
},
rest: {
type: String,
optional: true,
label: “Rest Duration”
},
exercises: {
type: [Exercise]
}
});
ProgramSchema = new SimpleSchema({
name: {
type: String,
label: “Program Name”
},
groups: {
type: [Group]
},
// author: {
// type: String,
// label: “Author”,
// autoValue: function () {
// return this.userId
// },
// autoform: {
// type: “hidden”
// }
// },
createdAt: {
type: Date,
label: “Created At”,
autoValue: function () {
return new Date()
},
autoform: {
type: “hidden”
}
}
});
SimpleSchema.debug = true
Programs.attachSchema( ProgramSchema );`
I am trying to do the following on a button click (just for testing purposes)
Programs.insert({ "name": "testprog", "groups": [ {"grpName": "testgrp", "rest": "testrest", "exercises": [ {"exername": "testexer","reps": "testreps","sets": 3,"notes": "testnotes"} ] } ] });
This fails with an error “insert failed: Error: Exercise is required”. SimpleSchema debug provides “SimpleSchema.clean: filtered out value that would have affected key “groups.$.exercises.$.exername”, which is not allowed by the schema”. So apparently, SimpleSchema is removing the value of the field exername. I am unable to figure out why it would do that.
Appreciate if you can point out the issue.