Reputation: 559
This is my simplified collection & its schema:
Comments = new Mongo.Collection('comments');
Comments.schema = new SimpleSchema({
attachments: {type: [Object], optional: true},
});
Comments.attachSchema(Comments.schema);
And this is my simplified method:
Meteor.methods({
postComment() {
Comments.insert({
attachments: [{type:'image', value:'a'}, {type: 'image', value:'b'}]
});
}
});
After invoking the method, this is the document I've got in MongoDB:
{
"_id" : "768SmgaKeoi5KfyPy",
"attachments" : [
{},
{}
]
}
The objects in the array don't have any properties! Now if I comment this line out:
Comments.attachSchema(Comments.schema);
and call the method again, the inserted document now seems correct:
{
"_id" : "FXHzTGtkPDYtREDG2",
"attachments" : [
{
"type" : "image",
"value" : "a"
},
{
"type" : "image",
"value" : "b"
}
]
}
I must be missing something fundamental here. Please enlighten me. I'm using the latest version of Meteor (1.2.1).
Upvotes: 1
Views: 43
Reputation: 4049
From the simple-schema docs:
If you have a key with type Object, the properties of the object will be validated as well, so you must define all allowed properties in the schema. If this is not possible or you don't care to validate the object's properties, use the blackbox: true option to skip validation for everything within the object.
So you'll want to add blackbox:true to your attachments' options.
Comments.schema = new SimpleSchema({
attachments: {type: [Object], optional: true, blackbox: true},
});
Upvotes: 1