6

我有以下SimpleSchema我試圖添加自定義驗證來驗證輸入重複的客戶名稱,但是每當我嘗試保存新客戶時,我會收到錯誤:流星使用namedContext將addInvalidKeys添加到AutoForm窗體返回錯誤

Exception in delivering result of invoking 'adminCheckNewCustomerName': TypeError: Cannot read property 'namedContext' of null

有人可以告訴我我在做什麼錯誤/錯過這裏驗證客戶名稱對重複記錄嗎?由於

schema.js:

AdminSection.schemas.customer = new SimpleSchema({ 
    CustomerName: { 
     type: String, 
     label: "Customer Name", 
     unique: true, 
     custom: function() { 
      if (Meteor.isClient && this.isSet) { 
       Meteor.call("adminCheckNewCustomerName", this.value, function(error, result) { 
        if (result) { 
         Customer.simpleSchema().namedContext("newCustomerForm").addInvalidKeys([{ 
          name: "CustomerName", 
          type: "notUnique" 
         }]); 
        } 
       }); 
      } 
     } 
    } 
}); 

UI.registerHelper('AdminSchemas', function() { 
    return AdminSection.schemas; 
}); 

form.html:

{{#autoForm id="newCustomerForm" schema=AdminSchemas.customer validation="submit" type="method" meteormethod="adminNewCustomer"}} 
    {{>afQuickField name="CustomerName"}} 
    <button type="submit" class="btn btn-primary">Save Customer</button> 
{{/autoForm}} 

collections.js:

this.Customer = new Mongo.Collection("customers"); 
+0

可否請你提供一個信息庫? –

回答

5

檢查collection2 code用於獲取連接到一個集合的模式:

_.each([Mongo.Collection, LocalCollection], function (obj) { 
    obj.prototype.simpleSchema = function() { 
    var self = this; 
    return self._c2 ? self._c2._simpleSchema : null; 
    }; 
}); 

這神祕的諧音_c2(在編程中的兩個硬的事情之一...)變成from attachSchema

self._c2 = self._c2 || {}; 
//After having merged the schema with the previous one if necessary 
self._c2._simpleSchema = ss; 

這意味着你已經忘了attachSchema或擺弄你的藏品。

解決:

Customer.attachSchema(AdminSchemas.customer); 
//Also unless this collection stores only one customer its variable name should be plural