Collection2 移除对象属性

Collection2 removes object properties

这是我的简化集合及其架构:

Comments = new Mongo.Collection('comments');

Comments.schema = new SimpleSchema({
    attachments: {type: [Object], optional: true},
});

Comments.attachSchema(Comments.schema);

这是我的简化方法:

Meteor.methods({
    postComment() {         
        Comments.insert({
            attachments: [{type:'image', value:'a'}, {type: 'image', value:'b'}]
        });
    }
});

调用方法后,这是我在MongoDB中得到的文档:

{
    "_id" : "768SmgaKeoi5KfyPy",
    "attachments" : [ 
        {}, 
        {}
    ]
}

数组中的对象没有任何属性! 现在,如果我将这一行注释掉:

Comments.attachSchema(Comments.schema);

再次调用该方法,插入的文档现在看起来是正确的:

{
    "_id" : "FXHzTGtkPDYtREDG2",
    "attachments" : [ 
        {
            "type" : "image",
            "value" : "a"
        }, 
        {
            "type" : "image",
            "value" : "b"
        }
    ]
}

我一定是漏掉了一些基本的东西。请赐教。 我正在使用最新版本的 Meteor (1.2.1)。

来自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.

所以您需要将 blackbox:true 添加到您的附件选项中。

Comments.schema = new SimpleSchema({
    attachments: {type: [Object], optional: true, blackbox: true},
});