0

我正在尝试发布一个集合,我想添加一个集合中不存在的字段。我可能在这里偏离轨道,但我认为我可以使用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;
});
4

1 回答 1

2

如果您想修改出版物中的文档,您应该使用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();
  });
});
于 2017-10-19T18:59:58.907 回答