Transforming Documents in Publication

I, for the life of me, can’t seem to understand or implement transformations in publications :confused:. I have this Patients collection:

Patients = new Mongo.Collection("patients", {
    transform: function (doc) {
        if (doc.work) {
            doc.company = Companies.findOne(doc.work.company_id);
        }
        return doc;
    }
});

Here’s an overview of part of the structure of the document.
Note, I’m trying to get the company name from work.company_id:

{
  profile: {
    name: 'Chris Blahblah'
    email:' chris@blah.com'
    ...
  },
  work: {
    company_id: ...
  }
}

I have my publication that gets all the patients:

Meteor.publish('patients.all', function () {
    return Patients.find({}, {fields: {
        'profile.name': 1,
        'work.company_id': 1
    }});
});

and I have my subscriptions like so:

Template.patient_row.onCreated(function () {
   this.subscribe('patients.all');
});

This, of course, doesn’t work. So how do I go about transforming my data?

1 Like