[Solved] Server-side modification of publication data


#1

To pre-sign AWS S3 URL’s for a safe GET request to S3 I need to add a property to each collection item on the server-side in the Meteor.publish method. My current approach is not working. My publication looks as follows:

Meteor.publish('projects', function () {

    var currentUserId = this.userId;

    if ( currentUserId ) {

        var projects = Projects.find();

        projects.forEach(function(o,i){

        o.signature = '&signature_string';
    });

    return projects;
}
else {

        this.ready();
    }
});

The problem is that .signature never arrives at the client-side. I spent the day on Google but didn;t not find anything useful so far and would be glad for a small hint :smile:

PS: I need to add this property on the server as each project has different S3 credentials and I cannot make these available on the client.


#2

You should use to transform option when calling Projects.find() (and return that cursor). See http://docs.meteor.com/#/full/find. Now you’re just returning an array, which is not how publications work. Either return a cursor (or an array of cursors), or use the various this methods.

EDIT: I didn’t see you were already returning the cursor, so that part is correct. Just replace the forEach part with the transform option (inside the find method).

EDIT 2: To quick with my answer, transform is not used for publications… Darn, ignore my answer. :frowning:


#3

I would think of non-reactive way like this.
If you want reactivity, you would need observer.
Consult Meteor.publish in documentation.

Meteor.publish('projects', function () {

    var currentUserId = this.userId;

    if ( currentUserId ) {

        var projects = Projects.find();

        projects.forEach(function(o,i){

        o.signature = '&signature_string';

        this.added('projects', o._id, o);
    });

    this.ready();
}

#4

#5

Hello shock!

Thank you for your solution! It works and results in the added property “signature” on the client side.

Does this method has any side effects? I assume an event is sent to the client to inform about the new added project. But will the project not be in the collection already as the publication returns the previous cursor?


#6

Well, I dont know if you are subscribing to some overlapping subscription.
I am used to create separate client only collections for these low level publish subscribes.
You can name it as u want and no problem to publish from 1 server side collection to 10 different client side collections (kinda read only).
Or use some of reactive solutions - even the documentation example is reactive.