2017-02-09 13 views
0

私は、コレクションの上に挿入するために、aldeed:autoform、aldeed:simple-schema、aldeed:collection2およびmdg:validated-methodを使用しています。これは、コレクションのスキーマであるSimpleSchemaの無効なキー "_id required"

<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> 

この

はオートとtempalteある

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

そして、これは、insertメソッドである:

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のデベロッパーコンソールでfolowingエラーが発生する:

01 "areas_agregar" コンテキストの

SimpleSchema無効キー: 配列[1] 0:オブジェクト 名: "_id" タイプ: "必須" 値:NULL プロト:オブジェクト 長さ:1 proto:Array [0]

エラーが表示されるのと同じように、_idフィールドの値を要求していますが、インサートの更新中です。意味がありません。

何が間違っている可能性がありますか?

+0

の場合あなたは '_id'を' optional:true'にして挿入し、 '_id'をMeteorが自動的に挿入します。 –

+0

はい!それはうまくいった。しかし、なぜ 'todos'のサンプルプロジェクトで '_id'フィールドに' _id optional:true'もないのですか? –

+0

そのプロジェクトは自動フォームを使用していますか? –

答えて

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

ありがとうミシェル。それはかなりエレガントなソリューションです。 –

関連する問題