简体   繁体   English

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

[英]Meteor 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, eg Collection.after.insert(function(userId, doc)). 当我使用它时,它可以正常工作,但是问题是当我需要收集钩子时,例如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? 如何使这项工作?

EDIT: 编辑:

I have read many hours about it and I think it might be connected with things like: - oplog - replicaSet 我已经阅读了许多小时,并且我认为它可能与以下内容有关:-oplog -plicateSet

But I am newbie to Meteor and can't find out what are those things about. 但是我是Meteor的新手,无法了解这些内容。 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. 我已经设置了MONGO_OPLOG_URL,并且在这里阅读时向数据库驱动程序添加了oplog参数: https ://medium.com/@lionkeng/2-ways-to-share-data-between-2-different-meteor-apps-7b27f18b5de9但没有改变。 And I don't know how to use this replicaSet, how to add it to the url. 而且我不知道如何使用此copysetSet,如何将其添加到url中。 Anybody can help? 有人可以帮忙吗?

You can also try something like below code, 您也可以尝试以下代码,

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

             }
        });

You can also have 'addedBefore(id, fields, before)' , 'changed(id, fields)' , 'movedBefore(id, before)' , 'removed(id)' 您还可以具有'addedBefore(id, fields, before)''changed(id, fields)''movedBefore(id, before)''removed(id)'

For more features goto link . 有关更多功能,请转到链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM