2017-06-26 2 views
0

この時点で私は実際にexample in the docsを模倣するために自分のコードを削除しました。{validate:false}が設定されていない限り、SimpleSchemaの挿入は役に立たないエラーで失敗します

私のような私のコレクションを定義しました:

var Categories = new Mongo.Collection('categories'); 

const CategorySchema = new SimpleSchema({ 
    title: { 
     type: String, 
     label: "Title", 
     max: 255, 
     optional: true 
    }, 
    created: { 
     type: Date, 
     label: "Date Category Created", 
     autoValue:() => { 
      if(this.isInsert){ 
       return new Date(); 
      } else if (this.isUpsert) { 
       return {$setOnInsert: new Date()}; 
      } 
     } 
    }, 
    updated: { 
     type: Date, 
     label: "Date Category Modified", 
     autoValue:() => { 
      if(this.isUpdate){ 
       return new Date(); 
      } 
     }, 
     optional: true 
    } 
}); 

Categories.attachSchema(CategorySchema); 

export { Categories }; 

The autovalue stuff is borrowed straight from the docs.

と挿入呼び出すための方法があります。私はこの問題を介して取得しようとしてきたように

Meteor.methods({ 
    'categories.insert'(title){ 
     check(title, String); 

     Categories.insert({title: title}, (err, res) => { 
      if(err){ 
       console.error("Category insert failed: " + err); 
      } 
     }); 
    }, 
... 

をスキーマ定義にルールを追加して削除しました。時にはcall stack exceededエラーが発生します。

Exception while simulating the effect of invoking 'categories.insert' TypeError: func is not a function 
    at http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:13027:18 
    at Array.forEach (native) 
    at doValidation (http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:13026:17) 
    at ValidationContext.validate (http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:12580:57) 
    at ns.Collection.doValidate (http://localhost:3000/packages/aldeed_collection2-core.js?hash=0564817159bbe9f8209b6a192a1fa2b2bbab924a:466:33) 
    at ns.Collection.Mongo.Collection.(anonymous function) [as insert] (http://localhost:3000/packages/aldeed_collection2-core.js?hash=0564817159bbe9f8209b6a192a1fa2b2bbab924a:260:25) 
    at ns.Collection.<anonymous> (http://localhost:3000/packages/matb33_collection-hooks.js?hash=d44fd0eb02806747e1bb0bdc3938463e415c63ec:402:19) 
    at ns.Collection.collection.(anonymous function) [as insert] (http://localhost:3000/packages/matb33_collection-hooks.js?hash=d44fd0eb02806747e1bb0bdc3938463e415c63ec:155:21) 
    at DDPCommon.MethodInvocation.categories.insert (http://localhost:3000/app/app.js?hash=580bf82f2f196202280f32cf5ffacbc930cd6b10:20073:14) 
    at http://localhost:3000/packages/ddp-client.js?hash=c9ca22092a3137a7096e8ab722ba5ab8eedb9aec:4076:25 TypeError: func is not a function 
    at http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:13027:18 
    at Array.forEach (native) 
    at doValidation (http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:13026:17) 
    at ValidationContext.validate (http://localhost:3000/packages/modules.js?hash=fefb674368e70344875cb9448e3ea784a880ffb0:12580:57) 
    at ns.Collection.doValidate (http://localhost:3000/packages/aldeed_collection2-core.js?hash=0564817159bbe9f8209b6a192a1fa2b2bbab924a:466:33) 
    at ns.Collection.Mongo.Collection.(anonymous function) [as insert] (http://localhost:3000/packages/aldeed_collection2-core.js?hash=0564817159bbe9f8209b6a192a1fa2b2bbab924a:260:25) 
    at ns.Collection.<anonymous> (http://localhost:3000/packages/matb33_collection-hooks.js?hash=d44fd0eb02806747e1bb0bdc3938463e415c63ec:402:19) 
    at ns.Collection.collection.(anonymous function) [as insert] (http://localhost:3000/packages/matb33_collection-hooks.js?hash=d44fd0eb02806747e1bb0bdc3938463e415c63ec:155:21) 
    at DDPCommon.MethodInvocation.categories.insert (http://localhost:3000/app/app.js?hash=580bf82f2f196202280f32cf5ffacbc930cd6b10:20073:14) 
    at http://localhost:3000/packages/ddp-client.js?hash=c9ca22092a3137a7096e8ab722ba5ab8eedb9aec:4076:25 
meteor.js?hash=27829e9…:930 Error invoking Method 'categories.insert': Internal server error [500] 

EDIT

矢印関数をスワップアウトした後:主に私が取得すると、次のです

methods.js

Meteor.methods({ 
    // Settings: create new category 
    'categories.insert': function(title){ 
     check(title, String); 

     Categories.insert({title: title}, function(err, res){ 
      if(err){ 
       console.error("Category insert failed: " + err); 
      } 
     }); 
    }, 
... 

settings.js

0123ショーンコメントで述べたようにあなたは、矢印の機能を使用している

+1

は、通常の関数宣言を使用して、矢印の機能を交換してみてくださいこの問題の詳細を読むことができます - それは違い、私は本当にこれが働くだろう期待していた – Sean

答えて

0
オリジナルのポストのようにまだ同じエラーを取得

Categories.js

var Categories = new Mongo.Collection('categories', options); 

const CategorySchema = new SimpleSchema({ 
    title: { 
     type: String, 
     label: "Title", 
     max: 255, 
     optional: true 
    }, 
    created: { 
     type: Date, 
     label: "Date Category Created", 
     autoValue: function(){ 
      if(this.isInsert){ 
       return new Date(); 
      } else if (this.isUpsert) { 
       return {$setOnInsert: new Date()}; 
      } 
     } 
    }, 
    updated: { 
     type: Date, 
     label: "Date Category Modified", 
     autoValue: function(){ 
      if(this.isUpdate){ 
       return new Date(); 
      } 
     }, 
     optional: true 
    } 
}); 

Categories.attachSchema(CategorySchema); 

。伝統的なfunction(){...}と交換してください。

あなたはhere

+0

が、運を作るかどうかを確認します。私は矢印関数を削除し、オブジェクトの関数のes6構文を '関数'という単語に置き換えました。同じエラー。 : – aviemet

+0

元の質問を試したバリエーションで更新できますか? – blueren

+0

編集済みのコードです。自分のバリデーションを書く必要がありますか? – aviemet

関連する問題