[SOLVED] Why tracker.autorun only run when I update MongoDB directly via Mongo Shell?

Hello,

I am bit confused with how tracker.autorun and subcription works

Basically I am trying to have a notification in the client that triggered when new data is added in MongoDB

My code as follow

Server Side code.

// this collection only available in server side
ActivityLog = new Mongo.Collection('activityLogs');

//server side method
Meteor.methods({
  //insert data
  logActivity: function(
          ALogUser, 
          ALogStatus, 
          ALogOperation, 
          ALogComponent, 
          ALogDescription) {
      ActivityLog.insert({
        user: ALogUser,
        status: ALogStatus,
        operation: ALogOperation,
        component: ALogComponent,
        description: ALogDescription,
        createdAt: new Date()
      });
    }
});
//publication
Meteor.publish('latestActivity', function () {
  Mongo.Collection._publishCursor( ActivityLog.find({}, {sort: {_id: -1}, limit: 1}), this, 'latestLogActivity');
  return this.ready();
});

Client Side code.

//local Mini Mongo. Only available in client
lastLog = new Mongo.Collection('latestLogActivity');
//subcribe to publication
Meteor.subscribe('latestActivity');
//it suppose to be ran automatically when the content is changed
  Tracker.autorun(function () {
    var activity = lastLog.find({}, {sort: {_id: -1}, limit: 1}).fetch()[0];
    console.log('is this executed');
  });
// this is public method that can be called from anywhere
createLog = function (status, operation, component, description) {
//get user
  var user = Meteor.users.find(
            {_id: Meteor.userId()},
            {'profile.name': 1})
            .fetch()[0]['profile']['name'];

// Calling server side method
  Meteor.call('logActivity', user, status, operation, component, description, function (err) {
    if (err) {
      console.log("error occurred on trying to insert activity log data on server. ", err);
    }
  });
}

My issue is that the tracker.autorun only run when insert the data directly to mongo (server side) via mongo shell.

But tracker.autorun does not run when the data is inserted via Meteor.call.

//suppose to be run automatically when the content is changed
  Tracker.autorun(function () {
    var activity = lastLog.find({}, {sort: {_id: -1}, limit: 1}).fetch()[0];
    console.log('is this executed');
  });

The data is updated, but the tracker is not triggered. Any hint what went wrong? Or maybe where I should start to debug this ?

Thanks

Why are you using a private API Mongo.Collection._publishCursor?

var activity = ActivityLog.find({}, {sort: {_id: -1}, limit: 1}).fetch()[0];

Looks like you’re assuming that _id is an incrementing field. When Mongo creates IDs, it starts them with the current date, but when Meteor creates them, I think they’re just entirely random, so the latest entry won’t be at the top. So you’ll just have to sort by createdAt instead.

Btw you could use ActivityLog.findOne({}, {sort: {createdAt: -1})

wew, that info is new to me. Thanks
now everything is solved