重用相同字段时的架构错误

时间:2016-08-23 00:26:34

标签: meteor simple-schema meteor-collection2

我有两个使用相同字段的独立模式。我试图创建schemaComponents,以便我可以在一个位置更新两个模式但是我收到错误Error: Invalid definition for school.$ field。当我使用它。我不确定我在这里做错了什么,我觉得这是允许的。

路径:SchemaComponents.js

SchemaComponents = {
  schools: {
    type: [String],
    optional: true,
    autoform: {
      options: [
        {label: "School One", value: 'SchoolOne'},
        {label: "School Two", value: 'SchoolTwo'},
        {label: "School Three", value: 'SchoolThree'},
      ]
    }
  }
};

路径:StudentSchema.js

import from '../components/SchemaComponents.js';
StudentSchema = new Mongo.Collection("studentSchema");

var Schemas = {};

Schemas.StudentSchema = new SimpleSchema({
    school: SchemaComponents.schools,
});

StudentSchema.attachSchema(Schemas.StudentSchema);

路径:TeacherSchema.js

import from '../components/SchemaComponents.js';
TeacherSchema = new Mongo.Collection("teacherSchema");

var Schemas = {};

Schemas.TeacherSchema = new SimpleSchema({
    school: SchemaComponents.schools,
});

TeacherSchema.attachSchema(Schemas.TeacherSchema);

1 个答案:

答案 0 :(得分:1)

您将SchemaComponent定义为简单的对象,而不是 SimpleSchema 对象。要重复使用schools定义,请执行以下操作:

let schoolsSchema = new SimpleSchema({
  schools: {
    type: [String],
    optional: true,
    autoform: {
      options: [
        {label: "School One", value: 'SchoolOne'},
        {label: "School Two", value: 'SchoolTwo'},
        {label: "School Three", value: 'SchoolThree'},
      ]
    }
  }
});

然后你可以这样做:

Schemas.TeacherSchema = new SimpleSchema({
  school: {
    type: schoolsSchema
  }
});