Subscribe in Template calling different Publish Method with similar find query


#1

I have two publish methods for two different templates, however when I subscribe to each from different templates. The first template from the first publish method returns correctly, while the second does not, it also returns the first publish method rather than the second.

It should go to note that each method search the same collection but for different results.

Example:

Sever Code

  Meteor.publish('collectionVariationOne', function() {
     return myCollection.find({ 'id' : valueOne});
});

  Meteor.publish('collectionVariationTwo', function() {
     return myCollection.find({ 'id' : valueTwo});
}); 

To recap if I subscribe and call find query on ‘collectionVariationOne’ in one template it works fine. But if I subscribe and call a find query on ‘collectionVariationTwo’ from a different template. IT ALSO RETURNS THE VALUE FROM ‘collectionVariationOne’.


#2

Can we see more code? The issue is elsewhere.


#3

Here is pretty simplified version

server

profileCollection = new Mongo.Collection('profileCollection');

Meteor.publish('userprofile', function() {
    return profileCollection.find({'userid' : this.userId});
});

Meteor.publish('profile', function(id) {
    return profileCollection.find({'userid' : id});
});

profileView Template

Meteor.subscribe('profile', Meteor.userId());
Template.profileView.helpers({
    profileHelper: () => {
        return profileCollection.find().fetch();
    }
});

userprofileView Template

Meteor.subscribe('userprofile');
Template.userprofileView.helpers({
    profileHelper: () => {
        return profileCollection.find().fetch();
    }
});

Then in the templates I have the obvious {{helper.objKeys}}, displaying the object.


#4

you also need to filter your client side collection with your userId, otherwise profileCollection.find() will return all published results from all subscriptions!

see Fetching data (Meteor guide)


#5

No go, that causes the query to result in blank. I think that would also defeat the purpose of Publish and Subscribe to not expose sensitive data.


#6

That does work if I remove the query from the server and have a empty find on the collection. But now that its not coming from the server doesn’t that make my data vulnerable from the client?


#7

are you using the autopublish packages (it’s included by default)?


#8

No I’m trying to moving my project to production, so I removed insecure and autopublish.


#9

just to clarify this code includes the changes mentioned above:

profileView Template

Meteor.subscribe('profile', Meteor.userId());
Template.profileView.helpers({
    profileHelper: () => {
        return profileCollection.find({ userId: Meteor.userId() }).fetch();
    }
});

userprofileView Template

Meteor.subscribe('userprofile');
Template.userprofileView.helpers({
    profileHelper: (id) => {
        return profileCollection.find({ userId: id }).fetch();
    }
});

if you use an “empty” find in your publish function all documents will be sent to the client, so you probably want to avoid that.


#10

Aah, it’s the same collection … You may need to close the first subscription and then open the second one. Or if you want, push both data sets once (your publish function can also return an array of cursors, or a single query for both data sets)


#11

I ended up using an empty query for the one that needed a dynamic value. Which is ok there is no sensitive data coming out of it.

Unless there is another security concern I’m not aware of.