Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change collection before publishing

Tags:

meteor

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

like image 825
Micha Roon Avatar asked Oct 22 '22 14:10

Micha Roon


2 Answers

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.

like image 100
mquandalle Avatar answered Nov 08 '22 10:11

mquandalle


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?

like image 36
Giant Elk Avatar answered Nov 08 '22 09:11

Giant Elk