Alvin Mahmudov
Alvin Mahmudov

Reputation: 103

Handling new records in meteor

I have "requests" collection, and I have created publications on server side and subscriptions on client side. How do I handle new records in mongodb? For example if a record was added to "requests" collection I want to get the record and perform some actions on client side. How do I do that?

Upvotes: 1

Views: 53

Answers (1)

coagmano
coagmano

Reputation: 5671

Depends on what action you want to take.

The simplest answer to this is to use a Tracker#autorun

Tracker.autorun(function() {
  MyCollection.find()
  // Do something here
}

Which will re-run whenever your collection changes.

If you are only interested in the new document you can use a Mongo.Cursor#observeChanges

MyCollection.find().observeChanges({
  added(id, fields) {
    //do something
  },
  changed(id, fields) {
    //do something
  },
  removed(id) {
    //do something
  },
});

Upvotes: 2

Related Questions