如何正确使用this.added
How to correctly use this.added
我正在尝试发布一个集合,我想添加一个集合中不存在的字段。我在这里可能偏离了轨道,但我认为我可以使用 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;
});
如果你想修改发布的文件,你应该使用Cursor.observe()
or 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();
});
});
我正在尝试发布一个集合,我想添加一个集合中不存在的字段。我在这里可能偏离了轨道,但我认为我可以使用 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;
});
如果你想修改发布的文件,你应该使用Cursor.observe()
or 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();
});
});