流星:JSON不';不匹配单纯形

流星:JSON不';不匹配单纯形,json,meteor,simple-schema,Json,Meteor,Simple Schema,我在我的meteor应用程序中收集了这个SimpleSchema Collection.attachSchema(new SimpleSchema({ title: { type: String }, slug: { type: String, unique: true }, language: { type: String, defaultValue: "en" },

我在我的meteor应用程序中收集了这个SimpleSchema

Collection.attachSchema(new SimpleSchema({
    title:                  { type: String },
    slug:                   { type: String, unique: true },
    language:               { type: String, defaultValue: "en" },
    'category.element':     { type: String, optional: true }
}));
我尝试插入这个JSON数据,但我得到
insert失败:错误:类别必须是getErrorObject的对象

{
    "_id" : "25uAB4TfeSfwAFRgv",
    "title" : "Test 123",
    "slug" : "test_123",
    "language" : "en",
    "category" : [
        {
            "element" : "Anything"
        }
    ]
}

我的JSON数据有什么问题?或者我的SimpleSchema出了什么问题。我可以更改这两个属性以匹配最佳方式。

最简单的解决方案是将
类别定义为架构中的对象数组:

Collection.attachSchema(new SimpleSchema({
    title:       { type: String },
    slug:        { type: String, unique: true },
    language:    { type: String, defaultValue: "en" },
    category:    { type: [Object], optional: true }
}));
这会让你放松的

如果您想更具体地了解
类别
的内容,则可以针对
类别
。例如:

CategorySchema = new SimpleSchema({
    element:     { type: String }
});

Collection.attachSchema(new SimpleSchema({
    title:       { type: String },
    slug:        { type: String, unique: true },
    language:    { type: String, defaultValue: "en" },
    category:    { type: [CategorySchema], optional: true }
}));

你需要先声明对象,比如

Collection.attachSchema(new SimpleSchema({
    ...,
    ....,
    category: {type: [Object], optional: true}
}));
之后,您可以扩展/定义对象字段,如

如果是数组对象([Object]),请使用“$”,如果只有对象,则不要使用“$”。
如果您不确定对象结构,请使用另一个参数
blackbox:true


如果我想在
类别中包含
元素:任何
(如JSON数据中所示),那么我必须定义一个子模式?你能给我举个例子吗?假设JSON数据是我需要的格式吗?比如
category:{type:[new SimpleSchema({element:{type:String,optional:true}})],optional:true}
Collection.attachSchema(new SimpleSchema({
    ....,
    ....,
    category: {type: [Object]},
    'category.$.element': {type: String}
}));
category: {type: [Object], blackbox: true}