node.jsmongodbmeteormeteor-collectionsmeteor-collection-hooks

collection hook not called on second application


I have an application written with Meteor 1.4 and run it on port 3000 (called A) know I want use A application database in another application (called B). In B console I set MONGO_URL like this:

export MONGO_URL=mongodb://localhost:3001/meteor

And then run that on port 5000. I have some collection hook on app A and also some collection hook on app B but hooks only called in app A.

For example in A we have:

collections.notes.after.update(function(userId, doc, fieldNames, modifier, options) {
    console.log("notes updated in A hook");
    console.log(doc);
}

And in B we have:

collections.notes.after.update(function(userId, doc, fieldNames, modifier, options) {
    console.log("notes updated in B hook");
    console.log(doc);
}

But it's only log notes updated in A hook.

How to fix it?


Solution

  • A collection hook runs only in the application where the change was made. Your app B hook won't run on a change from app A.

    If you want to do something in app B based on the underlying MongoDB data changing then you want to observe that collection:

    collections.notes.find().observe({
      changed(newDocument, oldDocument){
        console.log('Notes changed! Old doc: '+oldDocument+' to '+newDocument);
      }
    });