swennemen
swennemen

Reputation: 955

Deps.autorun based on chaning value in database.

I try to get a Deps.autorun function based on a changing value in my database. My goal is to run a function everytime this value changes, instead of having to make a lot of method calls in my code.

With Session.set and Session.get setting up Deps.autorun gives no problems and it's pretty straightforward.

My code (simplified);

newNotification = function() {

  var game = Games.find({}, {fields: {lastAction: 1}}).fetch();
  console.log('log: ' + game); // [object, object]
  console.log('log: ' + game._id); undefined
  console.log('log: ' + game.lastAction); undefined
}

Deps.autorun(function() {
  newNotification();
})

How would I accomplish something like this? Is there an other (better) way to do this, working with Session objects for example?

Upvotes: 0

Views: 59

Answers (1)

pahan
pahan

Reputation: 2453

you should take a look at observeChanges in the meteor documentation. try this.

Games.find({}, {
  fields: {
    lastAction: 1
  }
}).observeChanges({
  changed: function(id, fields) {
    newNotification();
  }
});

Upvotes: 1

Related Questions