Meteor React 发布已合并 collections

Meteor React publish merged collections

使用 Meteor (1.4.2.3) 和 React,我有 collection Objects,它有一个 itemId,它指的是 collection Items .

目前我在客户端订阅了 collection:

export default createContainer(() => {
  let objectsSub = Meteor.subscribe('allObjects');
  var objects = Objects.find({}, {
      transform: function (doc) {
        doc.item = Items.findOne({
          _id: doc.itemId
        });
        return doc;
      }
    }).fetch();
  return {
    objects: objects,
  }
}, App);

这很完美,但我认为在服务器端合并 collection 更优雅。然而,none 我找到的解决方案似乎有效

转换 collection 定义

const Objects = new Mongo.Collection('objects',
  {
    transform: function (doc) {
      doc.item = Items.findOne({
        _id: doc.itemId
      })
    }
  });

控制台给出:

Error: transform must return object

发布时转换

if (Meteor.isServer) {
  Meteor.publish('allObjects', function () {
    return Objects.find({}, {
      sort: { startedAt: -1 },
      transform: function (doc) {
        doc.item = Items.findOne({
          _id: doc.itemId
        });
        return doc;
      }
    });
  });
};

TypeError: Cannot read property 'name' of undefined

其中 nameItems

的 属性

我通常在发布时这样做:

  Meteor.publish('allObjects', function () {
    let cursor = Objects.find({}, {
      sort: { startedAt: -1 });
    });

    let transformData = (fields) => {
        fields.item = Items.findOne({
          _id: fields.itemId
        });

        return fields;
    };

    let handle = cursor.observeChanges({
        added: (id, fields) => {
            fields = transformData(fields);
            this.added('objects', id, fields);
        },
        changed: (id, fields) => {
            fields = transformData(fields);
            this.changed('objects', id, fields);
        },
        removed: (id) => {
            this.removed('objects', id);
        }
    });

    this.ready();

    this.onStop(() => {
        handle.stop();
    });
  }