meteor-collection2: Uncaught Error: After filtering out keys not in the schema, your object is now empty
This error occurs when an empty serialized data object (with keys set to the empty string) is passed to People.insert
. If I understand correctly, this is wrong, and should instead emit an error telling that the field fullname
and email
are required.
People = new Mongo.Collection("people")
Schema.People = new SimpleSchema({
fullname: {
type: String
},
alias: {
type: String
optional: true
},
email: {
type: String
max: 254
regEx: SimpleSchema.RegEx.Email
},
gender: {
type: String
},
phone: {
optional: true,
type: String
}
})
People.attachSchema(Schema.People)
People.insert({fullname: "", alias: "", email: "", password: "", repassword: ""})
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 18
- Comments: 16
Hi, I was facing the similar issue. I added schema directly to the collection as shown below, without using the attachSchema() method and it worked for me.
People.attachSchema(Schema.People) // remove this line and replace with below line.
People.schema = Schema.People;
hope it solves the issue. Happy Coding!
in case this helps - anyone - make sure you haven’t accidentally attached your schema before defining it 😃
MyCollection.attachSchema(MyCollection.schema);
must come after the
MyCollection.schema = new SimpleSchema({ … otherwise it will obviously be a blank schema.
@rikyperdana just cry and it will work! since this is a dumb error wtf do you expect us to do here @aldeed ?? thanks for all your work but this is so weird!!
This is because by default SimpleSchema cleaning does
removeEmptyStrings
and thenfilter
. So the props with string values become undefined, and then are removed because they are undefined. This is the default because it helps keep the database smaller (unlike SQL, in mongo null/empty props do take up space).If it happens that
filter
ends up removing all of the props, then you get this error.Cleaning defaults can be changed per schema: https://github.com/aldeed/simple-schema-js#set-default-cleaning-options
Or per operation: https://github.com/aldeed/meteor-collection2#inserting-or-updating-without-cleaning
Hi, the error may occur if you attempt to save empty data (obviously if you try to save field that is not in schema’s definition, error will be thrown), please at least in error’s message tell us the field’s name which fired the error or more accurate message