Micha Roon
Micha Roon

Reputation: 4009

change collection before publishing

I would like to add a property to the objects that get published to the client.

My publish function looks like that

Meteor.publish("forms", function() {
  return Forms.find();
});

I would like to do something like this

Meteor.publish("forms", function() {
  var forms = Forms.find();
  forms.forEach(function (form) {
     form.nbForms = 12;
  }

  return forms;
});

What I would like is that all the documents in forms have a new count attribute which gets sent to the client.

But this obviously does not work.

thank you for your help

Upvotes: 5

Views: 982

Answers (2)

Giant Elk
Giant Elk

Reputation: 5685

I think you need to do something similar to this Meteor counting example in Publish: How does the messages-count example in Meteor docs work?

I also posted a question here that may help once it's answered. Meteor has a this.added which may work, but I'm currently uncertain how to use it. Hence the question below: Meteor, One to Many Relationship & add field only to client side collection in Publish?

Upvotes: 1

mquandalle
mquandalle

Reputation: 2598

Not sure it will work in your case but you might use the new transform collection function introduced with Meteor 0.5.8

When declaring your collection, add this function as the second parameter :

Forms = new Meteor.Collection("forms", {
     transform: function(f) {
         f.nbForms = 12;
         return f;
     }
});

But this will be on both server and client. I don't know if there is a way to define a transform function in a publish context.

Upvotes: 2

Related Questions