Метеор использует namedContext для добавления InvalidKeys в форму автоформ, возвращающую ошибку
У меня есть следующая SimpleSchema, где я пытаюсь добавить пользовательскую проверку для проверки ввода двойного имени клиента, но всякий раз, когда я пытаюсь сохранить нового клиента, я получаю сообщение об ошибке:
Исключение в доставке результата вызова 'adminCheckNewCustomerName': TypeError: Невозможно прочитать свойство namedContext из 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");
1 ответ
Проверьте код 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