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

Meteor using namedContext to addInvalidKeys to an AutoForm form returning an error

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

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");

检查 collection2 code 以获取附加到 collection 的模式:

_.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 或摆弄了 collection 的 属性。

求解:

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