Remote collection - hooks don't work

I have to connect to the external database and get access to its collections. It works fine, when I use it, but the problem is when I need collection hooks, e.g. Collection.after.insert(function(userId, doc)). The hook is not being fired. I have following code:

// TestCollection.js

let database = new MongoInternals.RemoteCollectionDriver("mongodb://127.0.0.1:3001/meteor",
{
    oplogUrl: 'mongodb://127.0.0.1:3001/local'
});
let TestCollection = new Mongo.Collection("testCollection", { _driver: database });
module.exports.TestCollection = TestCollection;
console.log(TestCollection.findOne({name: 'testItem'})); // writes out the item correctly

// FileUsingCollection.js
import { TestCollection } from '../collections/TestCollection.js';
console.log(TestCollection.findOne({name: 'testItem'})); // writes out the item correctly second time

TestCollection.after.update(function (userId, doc) {
  console.log('after update');
}); // this is NOT being fired when I change the content of remote collection (in external app, which database I am connected)

How to make this work?

I have read many hours about it and I think it might be connected with things like:

  • oplog
  • replicaSet

But I am newbie to Meteor and can’t find out what are those things about. I have set MONGO_OPLOG_URL and I added oplog parameter to database driver as I read here: https://medium.com/@lionkeng/2-ways-to-share-data-between-2-different-meteor-apps-7b27f18b5de9
but nothing changed. And I don’t know how to use this replicaSet, how to add it to the url. Please help me.

Collection hooks do not monitor the database for changes but replace the original insert, update etc functions. So if you change data in the db directly or via an external source, the collection hooks do not know about it. For them to work properly you must use the insert, update etc methods in the app that uses the collection-hooks package. Maybe something like https://www.npmjs.com/package/mongojs-hooks might help you.

1 Like

I’ll try, but I am wondering about this: https://guide.meteor.com/structure.html#sharing-data , where we have:

The simplest approach is to point both applications at the same MONGO_URL and allow both applications to read and write from the database directly. This works well thanks to Meteor’s support for reactivity through the database. When one app changes some data in MongoDB, users of any other app connected to the database will see the changes immediately thanks to Meteor’s livequery.

It seems to be contrary with your answer.

It is not bc collection hooks are not built on top of livequery. You can use observers, that way you leverage meteors support for reactivity.

I tried to use observers also, but it didn’t work. I used the following code:

var observer = TestCollection.find({}).observeChanges({
    changed: function (id, fields) {
    console.log("after");
  }
});

EDIT:

Observer worked! I updated Meteor to 1.5.1 and cleaned up the code and now it’s fine. Thanks!

1 Like