如何正确使用this.added

时间:2017-10-18 02:10:28

标签: meteor meteor-publications

我试图发布一个集合,我想添加一个在集合中不存在的字段。我可能会偏离这里,但我想我可以使用this.added()向已发布的集合中添加字段。有人可以告诉我我做错了吗

Meteor.publish('job.view-open-to-candidate', function(jobCollectionId) {
  const job = Jobs.find({ _id: jobCollectionId }, {
    fields: {
      candidateApplication: 0
    }
  });

  this.added('job', jobCollectionId, {testNewField: 'test'})

  return job;
});

1 个答案:

答案 0 :(得分:1)

如果您想修改出版物中的文件,请使用Cursor.observe()Cursor.observeChanges()

最常见的模式是(需要修改):

Meteor.publish('job.view-open-to-candidate', function(jobCollectionId) {
  const publication = this;

  const handle = Jobs.find({ _id: jobCollectionId }, {
    fields: {
      candidateApplication: 0
    }
  }).observeChanges({
    added(_id, fields) {
      const newFields = fields;
      newFields.testNewField = 'test';
      publication.added('jobs', _id, newFields);
    },

    changed(_id, fields) {
      const newFields = fields;
      newFields.testNewField = 'test';
      publication.changed('jobs', _id, newFields);
    },

    removed(_id) {
      publication.removed('jobs', _id);
    },
  });

  this.ready();

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