Meteor使用namedContext将addInvalidKeys添加到AutoForm表单返回错误

MCh*_*han 6 javascript meteor meteor-autoform meteor-collection2 simple-schema

我有以下SimpleSchema,我试图添加自定义验证以验证输入重复的客户名称,但每当我尝试保存新客户时,我收到错误:

提供调用'adminCheckNewCustomerName'结果的异常:TypeError:无法读取null的'namedContext'属性

有人可以请告诉我我在做错了什么/在这里缺少来验证客户名称是否有重复记录?谢谢

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;
});
Run Code Online (Sandbox Code Playgroud)

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}}
Run Code Online (Sandbox Code Playgroud)

collections.js:

this.Customer = new Mongo.Collection("customers");
Run Code Online (Sandbox Code Playgroud)

Kyl*_*yll 5

检查collection2代码以获取附加到集合的模式:

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

这个神秘的谐音_c2(在编程中的两个坚硬的东西一个...)变成来自attachSchema:

self._c2 = self._c2 || {};
//After having merged the schema with the previous one if necessary
self._c2._simpleSchema = ss;
Run Code Online (Sandbox Code Playgroud)

这意味着您已经忘记attachSchema或摆弄了收藏品的财产.

要解决:

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