Re-subscribe doesn't remove old data on the client

My subscribe code (inside onCreated() hook):

instance.sub = instance.subscribe('company.adminsList', instance.limit.get(), instance.search.get(), errorHandler);
    return instance.autorun(() => {
        let limit = instance.limit.get();
        let search = instance.search.get();

        console.log('limit:', limit, 'search:', search)
        instance.sub = instance.subscribe('company.adminsList', limit, search, errorHandler);
        // old documents from first sub is not removed, always here. I check this through the browser console.
    });

My helper:

admins: function () {
        return Users.find({
            role: 'admin'
        }).fetch(); 
    },

So, old documents from the first subscription call are always on client. In example, by default my limit is 20; I change it to 40 => all OK, I change limit to 10 => I still have 20 documents on the client. Googling for hours, I didn’t find the answer.
My publication is working properly, I checked it:

Meteor.publish('company.adminsList', function (limit = 50, search = '') {
...
...
let cursor =Meteor.users.find({
        company: Meteor.user().company,
        role: 'admin',
      //  $or: or // needed for search
    }, {
        limit: limit,
        fields: fields,
        sort: {
            createdAt: 1
        },
    });

    console.log('found ', cursor.fetch().length) // All OK

    return cursor;
}

To implement search, now I’m using methods. It’s working great, but the problem is methods are not reactive.

I think you got your pattern slightly wrong. Don’t specify the autorun in the ready callback of your initial subscription. Instead just do this

return instance.autorun((computation) => {
    console.log(` firstRun=${computation.firstRun}, invalidated=${computation.invalidated}, recomputing=${computation._recomputing}`);
    let limit = instance.limit.get();
    let search = instance.search.get();

    console.log('limit:', limit, 'search:', search)
    instance.sub = instance.subscribe('company.adminsList', limit, search, errorHandler);
});

depending on how many items the publication pushes to the client and how large the documents in the publication are, there will be some delay for the old subscription documents to be removed from the client collection, so there will be an overlap of old and new documents on the client.

You can limit this a little by using the same search criteria on the client as you use in the subscription, or by making sure you somehow know the document _ids that belong to each subscription.

I have tinkered with that in my module jamgold:meteor-collection-count

1 Like

The problem is not the returned count. In my list (table, each

represents a user) there are always these 20 users. Always. I check the Users collection through my browser console. (Users is an alias for Meteor.users)

Have you tried

if(instance.sub) instance.sub.stop();
instance.sub = instance.subscribe('company.adminsList', limit, search, errorHandler);
1 Like

Ok, thanks for your clue abount instance.sub.stop().

At last, I’ve tried changing subscription inside my helper, and it works now.

'click .resub'(e,t){
        if(t.sub){
            t.sub.stop();
        }
        t.sub = t.subscribe('users.list.medics', limit); // 1 is limit
    },

Thanks again.