2017-08-08 49 views
1

我必须连接到外部数据库并访问其集合。当我使用它时,它工作正常,但问题是当我需要集合挂钩时,例如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 - replicaSet

但我是新手到流星也查不到那些事情是关于什么的。我已经设置了MONGO_OPLOG_URL,并且我在此处添加了oplog参数到数据库驱动程序:https://medium.com/@lionkeng/2-ways-to-share-data-between-2-different-meteor-apps-7b27f18b5de9 但没有任何更改。我不知道如何使用这个副本,如何将它添加到网址。任何人都可以帮忙?

+0

你在哪里添加钩子的代码在哪里? –

+0

它的整个指令是:'TestCollection.after.update(function(userId,doc)...'它应该加钩子 – blazej24

+0

对不起,我错过了最后一行的评论。'//这不是当我更改远程集合的内容时(在外部应用程序中,我连接了哪个数据库)''[matb33:collection-hooks](https://atmospherejs.com/matb33/collection-hooks)挂钩了流星应用程序的更新。它并没有观察到与其他应用绑定到同一个数据库的集合的变化,这是观察者可以在他们看数据库时有所帮助的地方,OTOH是一个观察者不能在*之前执行* hook。 –

回答

0

您也可以尝试像下面的代码,

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

      } 
     }); 

你也可以有'addedBefore(id, fields, before)''changed(id, fields)''movedBefore(id, before)''removed(id)'

更多的功能goto link

+0

它不工作,与更改的挂钩相关的功能不会触发 – blazej24

+0

观察者工作!我更新了流星到1.5.1并清理了代码,现在很好。 – blazej24