Search code examples
meteormeteor-publications

Using this.added in Meteor


I'm trying to transform a publication, and this is my code:

Meteor.publish('appointments.waiting', function () {
  var self = this,
  count = 0;

  Appointments.find().forEach(function (appointment) {
    var patients = Patients.find({_id: appointment.patient_id}).fetch();
    var first_name = patients[count].profile.first_name,
        middle_name = patients[count].profile.middle_name,
        surname = patients[count].profile.surname,    
        name = surname + ', ' + first_name + ' ' + middle_name;

    self.added('appointments', appointment.names, name);

  });

  self.ready();

});

When I console.log(name), I can see the name in full but I'm not quite sure how to use this.added to add the new data. How do I go about this? And if I do enter this new data, will it overwrite the older data?

If there's a better way to achieve this, I'd also like to know.


Solution

  • I believe what your code will do is to publish one, static set of appointments, and that should work. It cannot overwrite anything because it is creating something new (a new publication).

    So as such I don't see anything wrong with your code. But if you are after a reactive publication that changes then Appointments changes, then you will need to use observe or observeChanges instead of .forEach -- and then also start using this.changed, rather than only added (unless things never change), and this.removed.