-1

Good day! I am returning two cursors in a meteor publication. The code in question is here:

Meteor.publish('members.checkedIn', function() {

let organizationId = OrganizationService.getOrganizationId(this.userId);
if (!organizationId) return this.ready();

let timeclockQuery = {
  organizationId: organizationId,
  entityType: 'member',
  timeOut: null
};

let timeclocks = Timeclock.find(timeclockQuery).fetch();

let memberIds = _.map(timeclocks, tc => {
  return { _id: tc.entityId };
});

let memberQuery = { $or: memberIds };

return Timeclock.find(timeclockQuery).count() == 0 ? 
  this.ready() : [
    Timeclock.find(timeclockQuery),
    Member.find(memberQuery)
  ];

});

Everything works exactly as suspected, all though when in my code I clock out a member (Aka the timeOut field will return one result LESS than before because timeOut !== null the content on the page is not reactive. Meaning when I renavigate to this page, I see the expected change, but it is not happening 'live' per say. (The member query appears not to be changing once you are subscribed). Should I switch this to be 2 seperate subscriptions? Or can any of you see exactly what I'm doing wrong?

Thanks :)

Extra clarity: The only way to know if a member is 'in the store' is by querying for timeclocks where timeOut is null (Aka they haven't left yet)

1 Answers1

0

You are basically doing some kind of JOIN query here, as you lookup a variable which may change, but is not reactive, then you use it to define the query on your Collection: this won't be reactive to the variable changes.

For this to be reactive, the whole publication needs to reactively check for the change in your timer variable.

For this I use peerlibrary:reactive-publish which a package that does just that.

https://github.com/peerlibrary/meteor-reactive-publish

There are others you can check out.

The basic use is as follow:

Meteor.publish('subscribed-posts', function () {
  this.autorun(function (computation) {
    var user = User.findOne(this.userId, {fields: {subscribedPosts: 1}});

    return Posts.find({_id: {$in: user && user.subscribedPosts || []}});
  });
});

So no major change in your code except for the wrapping (and making sure the context is kept (use self = this and self inside the autorun.)

MrE
  • 19,584
  • 12
  • 87
  • 105