Counts always return the same number

Hello Everyone!

I’m new to Meteor, and I’ve spend a lot of hours today investigating the best way to achieve the same example explained in tmeasday:publish-counts package
Please, I need some help to achieve this:

I have a select dropdown list showing all available events, when the user select an event I want to get all invited persons in that event. I have a limit for pagination and in the SAME publication count the total number of results in the Selected Event.

This is the code in my server:

  Meteor.publish('people.byCommunityId', function(communityId, limit) {
    check(communityId, String);
    check(limit, Number);

    Counts.publish(
      this,
      `people.countsCheckedIn`,
      People.find({ communityId }, { noReady: true })
    );

    return People.find(
      { communityId },
      { sort: { firstName: 1 }, limit: Number(limit) }
    );
  });

This is the code from my 2 components connected to that publication:

Component 1: EventInfoView - show only the counts for the selected event (eventId)

export default withTracker(({ eventId, limit }) => {
  const peopleCountsHandle = Meteor.subscribe(
    'people.byCommunityId',
    eventId,
    limit
  );
  const loading = !peopleCountsHandle.ready();
  return {
    loading,
    peopleCheckedIn: Counts.get('people.countsCheckedIn'),
  };
})(EventInfoView);

Component 2: PersonList - Grid of persons in the selected event (eventId)

export default withTracker(({ eventId, limit }) => {
  const peopleHandle = Meteor.subscribe('people.byCommunityId', eventId, limit);
  const loading = !peopleHandle.ready();
  return {
    loading,
    people: People.find({}).fetch(),
  };
})(PersonList);

All is working fine in the PersonList component, is showing all the persons in the event with the limit.

The problem comes with the EventInfoView component because is showing always the same number (peopleCheckedIn is always the same)

What am I doing wrong?

Thanks

Hi @nicopixel, welcome to the forums!

The first thing I notice is the issue is on this line:

      People.find({ communityId }, { noReady: true })

Here, you are passing noReady to find, when the docs specify it should be passed to Counts.publish as a fourth argument like so:

  Counts.publish(this, 'posts', Posts.find(), { noReady: true });

Which, for your code would be:

    Counts.publish(
      this,
      `people.countsCheckedIn`,
      People.find({ communityId }, 
      { noReady: true },

If that doesn’t fix it, I would next try to isolate the issue by running it outside of a component and see if it is updating:

  const peopleCountsHandle = Meteor.subscribe(
    'people.byCommunityId',
    "some known eventId",
    20
  );
Tracker.autorun(function() {
  console.log("count: ", Counts.get('people.countsCheckedIn'))
})

Which should initially log 0 then the initial count, then try updating the collection to change the count and see if it logs.
Then you can figure out if it’s a Meteor issue or a React issue

Thanks for your response.

Yes, I write the code wrong, now the noReady option was corrected in my code but I paste in the question wrong. This is the code for my server:

if (Meteor.isServer) {
  Meteor.publish('people.byCommunityId', function(communityId) {

    Counts.publish(
      this,
      `people.countsCheckedIn${communityId}`,
      People.find({ communityId }),
      { noReady: true },
    );

    return People.find(
      { communityId },
      { sort: { firstName: 1 } },
    );
  });
}

I tried to isolate the code outside of the component, this is the Snippet that I’ve used:

Meteor.subscribe('people.byCommunityId', 'zhFAfoGE9BrNjBMpC');
Tracker.autorun(function() {
  console.log("count from outside of the component: ", 
  Counts.get('people.countsCheckedIn'))
})

The result is 0 the first time and then the number is 179 and never change, it’s seems that the problem persists. I can’t understand what is happening.

Maybe is related with this problem but if you look at the attached image

When you change and select the event for the first time the server returns 179 items and if you change the event again the the client receive 359 items (179*2) and after that remove the old 179 items.

It seems that there is an interval of time in the client where he has both old and new results at the same time. I think this is related to my error.

Thank you very much.

1 Like