Meteor live update not working as expected

When I make changes to the database that affect UserProfiles, the client side is updated. For instance, if I change a weekday from true to false the UserProfile will appear or disappear on my computer screen. However, when I make a change to Attendances, for instance, create a new attendance record which should make a UserProfile appear nothing happens unless I reload the page. I assume it’s to do with studnentUserIds not being rerun.

How do I make Meteor notice these changes?

This is being built using Meteor, React and mongoDB.

Meteor.publish('teacher.AdminDashboardContainer.userProfiles', function getStudentUserProfiles(rollCallDate) {
  if (this.userId) {
    const start = new Date(moment(rollCallDate).startOf('day').toISOString());
    const end = new Date(moment(rollCallDate).endOf('day').toISOString());
    const weekday = `student.days.${moment(rollCallDate).format('dddd').toLowerCase()}`;

    const studentUserIds = Attendances.find({
      $and: [
        { createdAt: { $gte: start, $lt: end } },
      ],
    }).map(attendance => attendance.studentUserProfileId);

    return UserProfiles.find({
        $or: [
          { _id: { $in: studentUserIds } },
          {
            $and: [
              { [weekday]: true },
            ],
          },
        ],
      }),
  }
  // user not authorized. do not publish secrets
  this.stop();
  return false;
});

Yep, studentUserIds is not being recalculated when a new Attendances document is inserted. The publication function only runs once, and the reactivity is based strictly on the db cursor returned by the publication on its first run.

Check out this package for a solution https://github.com/peerlibrary/meteor-reactive-publish.

1 Like