No Reactivity in Method


#1

Hi, MeteorFanatics,

I have a problem.
For my project, I needed to aggregate datas, to retrieve by weeks the things.
And it’s worked.

But when now, after the aggregation, I would like to add some datas to collection ( it’s working),
But there’s no more reactivity ! I have to reload the page to see the new added datas.

If there’s someone who want to help, it will save my life

git clone the project here

###Packages
simple:reactive-method
autoform
etc…

###both/collections/hours.js

Hours = new Mongo.Collection("hours");

Hours.attachSchema(new SimpleSchema({
  createdBy: {
    type: String,
    autoValue: function() {
      return Meteor.userId()
    }
  },
  date: {
    type: Date,
    autoform: {
      afFieldInput: {
        type: "bootstrap-datepicker"
      }
    }
  },
  workinghour: {
    type: Number,
    label: "Hours",
    min: 1,
    decimal: true,
    autoform: {
      step: "0.25"
    }
  },
  class: {
    type: String,
      label: "Class",
  },
  project: {
    type: String,
    label: "Project",
  },
  summary: {
    type: String,
    label: "Comments",
    optional: true,
    min: 5,
    max: 1000
  }
}));

Hours.allow({
  insert: function(userId, doc) {
    return true;
  },
  update: function(userId, doc, fields, modifier) {
    return true;
  },
  remove: function(userId, doc) {
    return true;
  }
});

if (Meteor.isServer) {
  Meteor.publish(null, function() {
    return Hours.find();
  });


}

###server/methods/methods.js

if (Meteor.isServer) {

Meteor.methods({
groupedByWeek: function() {

  var query = [{
    $group: {
      _id: {
        $week: "$date"
      },
      total: {
        $sum: 1
      },
      totalhour: {
        $sum: "$workinghour"
      },
      weeklyData: {
        $push: {
          date: "$date",
          workinghour: "$workinghour",
          class: "$class",
          project: "$project",
          summary: "$summary"
        }
      }
    }
  }, {
    "$sort": {
      _id: -1
    }
  }, {
    "$limit": 50
  }]; // This Query will return data grouped by week number. date is the field with date
  return Hours.aggregate(query);
}
})
}

###client/views/hours.js

if (Meteor.isClient) {

Meteor.subscribe('allUsers');

// List WeeklyHours
Template.showHours.helpers({
week: function() {
  console.log(ReactiveMethod.call('groupedByWeek'));
  return ReactiveMethod.call('groupedByWeek');
}
});

###client/views/hours.html

<template name="showHours">
	<table id="hours-table" class="table-fluid table-light table-hover">
		<thead>
			<tr>
				<th class="h6 date">Date</th>
				<th class="h6 time">Time</th>
				<th class="h6 category">Category</th>
				<th class="h6 project">Project</th>
				<th class="h6 comments">Comments</th>
				<th class="buttons"></th>
			</tr>
		</thead>
		<tbody>
		{{#each week}}
			<tr>
				<td class="h6 day-separator" colspan="6">
					{{_id}}th week | #{{total}} | {{totalhour}}h
				</td>
			</tr>
					{{#each weeklyData}}
					<tr>
						<td valign="middle">{{formatDateHours date}}</td>
						<td valign="middle">{{workinghour}}h</td>
						<td valign="middle">{{class}}</td>
						<td valign="middle">{{project}}</td>
						<td valign="middle">{{summary}}</td>
						<td valign="middle" align="right">
							<div class="table-icon-buttons">
								<i class="fa fa-pencil-square-o edit"></i>
								<i class="fa fa-times delete"></i>
							</div>
						</td>
					</tr>
					{{/each}}

		{{/each}}
		</tbody>
	</table>


</template>