【问题标题】:Collection2 relationship error due to missing collection object由于缺少集合对象,Collection2 关系错误
【发布时间】:2015-10-07 03:01:47
【问题描述】:

我正在尝试在 2 个集合之间创建关系,但其中一个集合无法在另一个集合中引用。具体来说,我有 2 个集合:站点和内容类型。它们包括以下内容:

// app/lib/collections/sites.js    
Sites = new Mongo.Collection('sites');

Sites.attachSchema(new SimpleSchema({
  name: {
    type: String,
    label: "Name",
    max: 100
  },
  client: {
    type: String,
    label: "Client",
    max: 100
  },
  created: {
    type: Date,
    autoValue: function() {
      if (this.isInsert) {
        return new Date;
      } else if (this.isUpsert) {
        return {$setOnInsert: new Date};
      } else {
        this.unset();  // Prevent user from supplying their own value
      }
    }
  }
}));

这是 ContentTypes 集合:

// app/lib/collections/content_types.js
ContentTypes = new Mongo.Collection('content_types');

ContentTypes.attachSchema(new SimpleSchema({
  name: {
    type: String,
    label: "Name",
    max: 100
  },
  machineName: {
    type: String,
    label: "Machine Name",
    max: 100
  },
  site:{
    type: Sites
  },
  created: {
    type: Date,
    autoValue: function() {
      if (this.isInsert) {
        return new Date;
      } else if (this.isUpsert) {
        return {$setOnInsert: new Date};
      } else {
        this.unset();  // Prevent user from supplying their own value
      }
    }
  }
}));

当我将站点引用添加到 ContentTypes 架构时,应用程序崩溃并出现以下错误:

ReferenceError:未定义站点 在 lib/collections/content_types.js:32:11

除了this 之外,我没有太多运气在collection2 中找到关系文档。看起来那里引用的格式应该基于this thread 工作。

【问题讨论】:

    标签: meteor iron-router meteor-collection2


    【解决方案1】:

    这是由于 Meteor 加载文件的顺序。请参阅文件加载顺序部分here

    有几个加载顺序规则。它们依次应用于应用程序中的所有适用文件,优先级如下:

    1. HTML 模板文件总是在其他所有内容之前加载
    2. 以 main 开头的文件。最后加载
    3. 接下来会加载任何 lib/ 目录中的文件
    4. 接下来会加载路径更深的文件
    5. 然后按整个路径的字母顺序加载文件

    例如将 app/lib/collections/sites.js 重命名为 app/lib/collections/a_sites.js,并且在加载 content_types.js 文件时会定义 Sites 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-07
      • 2021-12-19
      • 2016-09-27
      • 2015-11-28
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      相关资源
      最近更新 更多