Chris
Chris

Reputation: 3705

Overlapping subscription does not publish all fields

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"}

Upvotes: 3

Views: 147

Answers (1)

MrE
MrE

Reputation: 20778

I have seen this before.

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

Upvotes: 1

Related Questions