Meteor 在使用简单架构的自定义验证中调用方法后返回无效键

Meteor 在使用简单架构的自定义验证中调用方法后返回无效键,meteor,meteor-autoform,meteor-collection2,Meteor,Meteor Autoform,Meteor Collection2,因此,到目前为止,我对Collection2/Simple Schema的经验充其量是基本的。现在,我试图根据类型集合中的文档验证“类型”的值 @Data = new Mongo.Collection 'data' Data.attachSchema new SimpleSchema 'types': 'type': [String] 'label': 'Types' 'custom': -> if @isSe

因此,到目前为止,我对Collection2/Simple Schema的经验充其量是基本的。现在,我试图根据类型集合中的文档验证“类型”的值

@Data = new Mongo.Collection 'data'
Data.attachSchema new SimpleSchema
    'types':
        'type': [String]
        'label': 'Types'
        'custom': ->
            if @isSet
                Meteor.call 'isType', @value, (error, result) ->
                    if !result
                        Data.simpleSchema().namedContext('admin_update').addInvalidKeys [
                            'name': 'types'
                            'type': 'notAllowed'
                        ]
    'otherField':
        'type': Number
        'label': 'Other Field'
        'optional': true

到目前为止,我的isType方法一直在正确地验证值,但不管它返回true还是false,它都会存储值(即使表单会短暂地闪烁错误消息)。我认为我对自定义验证掌握得不够好,无法弄清楚如何正确执行此操作,因此,任何和所有帮助都将不胜感激,即使它只是将我推向了正确的方向。

您可以使用
allowedValues
字段,该字段可以接受数组或函数。使用函数

MyCollection.attachSchema(new SimpleSchema({
  types: {
    type: [String],
    allowedValues: function () {
      // assuming "Types" is the collection.
      return Types.find().map(function (doc) {
        return doc._id;
      });
    }
  }
}));

这将在后端进行验证,这样发布和订阅就不会成为问题。

出于某种原因,我从未使用过AllowedValue函数。我尝试了你上面建议的地图,并加入了订阅,因为我正在处理自动表单。现在的问题是,由于autoform检测allowedValue的值,它会将多个空白文本字段转换为multiselect字段,并且不会使用任何值填充该select,即使这些值已正确返回。我最好的猜测是它与异步调用有关,但是控制台日志会在第一个结果中立即显示所有正确的结果,所以我甚至不知道发生了什么。