Issue inserting to collection


#1

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.


#2

Two words: “case sensitive”. You used “exerName” in one place and “exername” in the other. :slight_smile:


#3

Indeed!! This is what happens when you don’t copy-paste!! I cannot believe I looked at the schema definition so many times and still did not catch it. Thank you!!