流星远程收集 - 钩子不起作用



我必须连接到外部数据库并访问其集合。当我使用它时,它工作正常,但问题是当我需要集合钩子时,例如 Collection.after.insert(function(userId, doc((。钩子没有被发射。我有以下代码:

// 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)

如何做到这一点?

编辑:

我已经阅读了很多小时关于它的内容,我认为它可能与以下事情有关: - oplog - 复制集

但我是流星的新手,找不到这些东西是关于什么的。我已经设置了MONGO_OPLOG_URL,并在我在这里阅读时将oplog参数添加到数据库驱动程序中:https://medium.com/@lionkeng/2-ways-to-share-data-between-2-different-meteor-apps-7b27f18b5de9 但没有任何改变。而且我不知道如何使用这个副本集,如何将其添加到 url 中。有人可以帮忙吗?

你也可以尝试下面的代码,

var observer = YourCollections.find({}).observeChanges({
added: function (id, fields) {
}
});

您还可以有'addedBefore(id, fields, before)''changed(id, fields)''movedBefore(id, before)''removed(id)'

有关更多功能,请转到链接。

最新更新