6

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

传递调用“adminCheckNewCustomerName”的结果时出现异常:TypeError:无法读取 null 的属性“namedContext”

有人可以告诉我我做错了什么/在这里遗漏了以验证客户名称与重复记录吗?谢谢

架构.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;
});

表单.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}}

集合.js:

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

1 回答 1

5

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

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

这个神秘的同音词_c2(编程中的两件难事之一......)来自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
于 2015-09-08T15:02:13.277 回答