2017-02-09 126 views
0

我正在使用aldeed:autoform,aldeed:simple-schema,aldeed:collection2和mdg:validated-method用于对集合执行插入操作。SimpleSchema无效密钥“_id required”

这是自动窗体的tempalte:

<template name="Areas_agregar"> 
    {{> Titulos_modulos title="Areas" subtitle="Agregar" cerrar=true}} 
    {{ 
    #autoForm 
    collection=areasColecction 
    id="areas_agregar" 
    type="method" 
    meteormethod="areas.insert" 
    }} 
    {{> afQuickField name='nombre'}} 
    {{> afArrayField name='subareas'}} 

    <button type="submit">Save</button> 

    <button type="reset">Reset Form</button> 
    {{/autoForm}} 
</template> 

这是集合的模式:

Areas.schema = new SimpleSchema({ 
    _id: { 
     type: String, 
     regEx: SimpleSchema.RegEx.Id 
    }, 
    nombre: { 
     type: String, 
     label: 'Nombre' 
    }, 
    subareas: { 
     type: [String], 
     label: 'Subareas' 
    } 
}); 

这是插入方法:

const AREA_FIELDS_ONLY = Areas.simpleSchema().pick(['nombre', 'subareas', 'subareas.$']).validator({ clean: true, filter: false }); 

export const insert = new ValidatedMethod({ 
    name: 'areas.insert', 
    validate: AREA_FIELDS_ONLY, 
    run({ nombre, subareas }) { 
     const area = { 
      nombre, 
      subareas 
     }; 
     Areas.insert(area); 
    }, 
}); 

而我在Chrome的开发控制台中出现以下错误:

间为 “areas_agregar” 上下文

SimpleSchema无效的键: 阵列[1] 0:对象 名: “_id” 类型: “必需的” 值:空 :对象 长度:1 原型:Array [0]

就像错误显示,问我为_id字段的值,但我在插入更新,它没有任何意义。

任何想法可能会出错?

+0

如果你使'_id''可选:true',那么你的插入将工作,流星会自动插入'_id' –

+0

是的!这工作。但是为什么在'todos'示例项目中,'_id'字段中没有'_id optional:true'? –

+0

该项目是否使用autoform? –

回答

0

autoform将模式中所需的密钥视为在输入中不需要的密钥,该密钥不适用于_id密钥。

如果使_id可选:true,那么您的插入会工作和流星会自动插入_id或者您可以使用模式的变化对于其省略了完全的_id键自动窗体:

let schemaObject = { 
    nombre: { 
    type: String, 
    label: 'Nombre' 
    }, 
    subareas: { 
    type: [String], 
    label: 'Subareas' 
    } 
}; 
Areas.formSchema = new SimpleSchema(schemaObject); // use for form 
schemaObject._id = { 
    type: String, 
    regEx: SimpleSchema.RegEx.Id 
}; 
Areas.collectionSchema = new SimpleSchema(schemaObject); // use for collection 
+0

谢谢米歇尔。这是一个非常优雅的解决方案。 –