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

标签 javascript meteor meteor-autoform meteor-collection2 simple-schema

我有以下 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用于获取附加到集合的模式:

_.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

关于javascript - Meteor 使用 namedContext 将 InvalidKeys 添加到返回错误的 AutoForm 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32380790/

相关文章:

递归函数中的 JavaScript promise

javascript - 在另一个内部动态编译和运行 native react 应用程序

javascript - 将 JSON 内容加载到 meteor 模板中

meteor - MDG ValidatedMethod 与 Aldeed Autoform : "_id is not allowed by the schema" error

javascript - 谷歌应用程序脚本 : Automated reporting with Google Analytics

javascript - 客户端未在 Socket.IO 中接收事件

javascript - meteor :在特定上下文中渲染模板

javascript - 使用来自 Typescript 的 react-meteor-data

javascript - Meteor:Accounts.createUser 抛出异常:meteor.roles.$name_1 dup key

javascript - 有没有办法用meteor.js autoform包显示当前保存的状态?