3

When I have two subscriptions that limit the fields to publish it does not do a union between the two

This should publish only the avatar.

Meteor.publish('userStatisticsByYear', function(year) {
    check(year, Number);
    this.unblock();

    var userStats = UserStatistics.find({year: year},{sort: {count: -1}, limit: 5});
    var userIds = userStats.map(function(u) {
        return u.userId;
    });

    return [
        Meteor.users.find({_id: {$in: userIds}},{fields: {username: 1, "profile.avatar": 1}}),
        ProfileImages.find({owner: {$in: userIds}}),
        userStats
    ];
});

And this subscription publishes more details of the profile. Which is fine under some routes.

Meteor.publish('getUserProfile', function(userId) {
    this.unblock();

    if (!this.userId) {
        this.ready();
        return;
    }

    return [
        Meteor.users.find({_id: userId}, {fields: {profile: 1}}),
        ProfileImages.find({owner: userId})
    ]
});

The problem is that if I subscribe to both only "profile.avatar" is being published. But not the extra fields from "getUserProfile"

The output of the console:

Meteor.subscribe('userStatisticsByYear')
Object {subscriptionId: "aQFv4HkGDKx54gJLq"}

Meteor.users.findOne("KABEf7SzNCmQapXND").profile
Object {avatar: "NQokwm9bHgfrMtKLY"}

Meteor.subscribe('getUserProfile','KABEf7SzNCmQapXND')
Object {subscriptionId: "hemH2NF88vwd3AkHv"}

Meteor.users.findOne("KABEf7SzNCmQapXND").profile
Object {avatar: "NQokwm9bHgfrMtKLY"}
Chris
  • 3,581
  • 8
  • 30
  • 51
  • I have faced the same problem. But I solved it by using the `reywood:publish-composite` package. It allows us to push data into different collections so that there is no conflict. _This is probably not the best way to solve it but its the only way I know._ – Soubhik Mondal Feb 16 '16 at 15:33
  • publish-composite is not really an option for me as it iterates through each element which makes it quite slow on big queries. – Chris Feb 17 '16 at 07:59
  • This [SO thread](http://stackoverflow.com/questions/12632452/publishing-subscribing-multiple-subsets-of-the-same-server-collection) might be of use. – Soubhik Mondal Feb 17 '16 at 09:37
  • How you sure it doesn't union? – asingh Feb 19 '16 at 13:33

1 Answers1

1

I have seen this before.

if you use Meteor.users.find({"_id":"KABEf7SzNCmQapXND"}).profile instead of findOne, I believe it should work.

MrE
  • 19,584
  • 12
  • 87
  • 105
  • by the way, i think the explanation is that with findOne you get a record while with find you get a reactive cursor. I bet if you changed the order of your subscriptions, findOne may work, but then i may be unpredictable, so better use find and be sure it will be updated when the data is updated. – MrE Feb 20 '16 at 21:14