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