How to ‘transform’ data returned via a Meteor.publish?

While you can’t directly use transforms, there is a way to transform the result of a database query before publishing it. This is what the “publish the current size of a collection” example describes here.

It took me a while to figure out a really simple application of that, so maybe my code will help you, too:

Meteor.publish("publicationsWithHTML", function (data) {
    var self = this;
    Publications
        .find()
        .forEach(function(entry) {
            addSomeHTML(entry);  // this function changes the content of entry
            self.added("publications", entry._id, entry);
        });
    self.ready();
});

On the client you subscribe to this:

Meteor.subscribe("publicationsWithHTML");

But your model still need to create a collection (on both sides) that is called ‘publications’:

Publications = new Meteor.Collection('publications');

Mind you, this is not a very good example, as it doesn’t maintain the reactivity. But I found the count example a bit confusing at first, so maybe you’ll find it helpful.

Leave a Comment