2017-08-08 43 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