1

I am trying with very basic default application of Meteor leaderboard. I am want to do some process on every update on my page load. Tracker.autorun is calling only for first time but not for every updates. The templates are refreshing automatically but only the Autorun is not executing. I tested autorun with both autopublish and non autopublish mode but still it doesn't work. I am using meteor Meteor 1.1.0.2. Any solution?

if (Meteor.isServer) {
Meteor.publish("tasks", function () {

    return Tasks.find();


});}



if (Meteor.isClient) {

Meteor.subscribe("tasks", function () {

    console.log("On subscribe");

});

Tracker.autorun(function () {

    console.log("On Load");
});}

1 Answers1

0

If you want to use an autorun block, you need to put inside the autorun the reactive element that will trigger it. In your case, it would then be:

Tracker.autorun(function () {
    var myCursor = Tasks.find().fetch();
    console.log("On Load");
});}

Read this to learn more about reactivity mechanisms.To make it cleaner, you could also attach the autorun to your template (see the link)

Billybobbonnet
  • 3,156
  • 4
  • 23
  • 49
  • Still not working. Used this link also http://stackoverflow.com/questions/29443513/meteor-tracker-autorun-observechanges-collections-not-working-as-expected. still not working – Prasanjeet Debnath Dec 17 '15 at 15:21
  • well, `instance` is just the name of the variable he uses for `this` within the `onCreated`, `this` refering to the template instance. Make sure your publication is working by typing in the console `console.table(Tasks.find().fetch())`. Also note that the autorun will rerun only if the content of the *published* data has changed – Billybobbonnet Dec 17 '15 at 15:29
  • Meteor.publish("tasks", function () { console.log("publishing"); return Tasks.find(); }); Publishing is not executing on every update but templates are updating smoothly – Prasanjeet Debnath Dec 17 '15 at 15:53
  • you don't resubscribe, it's normal that you are not republishing, i.e. not seeing your "publishing" log. The reactivity is not supposed to rerun the publish every time the published data change, it is supposed to update the returned cursor or array of cursor. – Billybobbonnet Dec 17 '15 at 16:10
  • It started working by updating var myCursor = Tasks.find() to var myCursor = Tasks.find().fetch(); Thanks. – Prasanjeet Debnath Dec 18 '15 at 06:23
  • Glad it worked. If your problem is solved, please consider validating my answer. – Billybobbonnet Dec 18 '15 at 09:00