Meteor集合观察用户活动日志(Meteor collection observe user activity log)

嗨,我正在尝试观察集合的变化,并保存更改日志,我在创建集合后立即设置观察,问题是,我无法弄清楚如何知道哪些用户做了更改,我试过了将观察者放在发布方法上,但它生成多个观察者并在只做了一件事时在日志上创建了许多条目,之后我将句柄保存到观察者以防止多个观察者,但后来无关紧要用户我登录,日志保存在第一个订阅发布的用户的名称中,我不想为每个用户创建一个观察者并动态删除观察者句柄,我很确定这将是在服务器上真的很激烈,必须有更好的方法。 也许在客户端设置观察者? 但我不希望客户端有权访问日志集合。

Products = new Mongo.Collection("products"); Log = new Mongo.Collection("log"); var initializing = true; Products.find().observe({ added: function(newDocument) { if (initializing) return; //how to get user here? Log.insert({ idUser: ???, date: new Date(), document: newDocument }); } }); initializing = false;

这是我的第一次尝试,这是我在发布时尝试的方式:

var productsObserveHandle; Meteor.publish('products', function() { var initializing = true; if (!productsObserveHandle) { productsObserveHandle = Products.find().observe( { added: function (document) { if (initializing) return; var userProfile = Meteor.users.findOne({_id:this.userId}).profile; Log.insert( { idUser: this.userId, userProfile: userProfile, date: new Date(), eventType: "added", document: document }); }.bind(this) }); } initializing = false; ... });

这个几乎得到了它,但它始终将日志保存为同一个用户,无论我尝试多少个不同的用户,因为已经创建了观察者句柄。

更新:好的,我想我到了某个地方,我在收集订阅后在客户端设置了观察,然后added , changed , removed所有观察方法调用流星方法,保存日志并获取用户Meteor.userId()和它的工作原理,但现在交易是,当您注销和登录时,minimongo数据库会更新,但订阅是相同的(我在Meteor.startup上进行所有订阅,在客户端),所以我无法阻止观察者并在更新后重新启动它,是否有办法知道由于帐户更改而何时更新了minimongo? 我可以尝试在用户注销后删除观察者并在登录后等待超时以再次设置观察者,但我发现这有风险,因为更新minimongo可能需要花费更多时间而不是我设置的超时,并且最后一次插入可能会泄漏到更改日志。

Hi I'm trying to observe changes on a collection, and save a changes log, I set up the observe right after creating the collection, the problem is, I can't figure out how to know which user did the changes, I tried putting the observer on the publish method, but it generates multiple observers and creates many entries on the log when only one thing was done, I saved the handle to the observer after that to prevent multiple observers, but then doesn't matter what user I log in, the log is saved in the name of the first user that got to subscribe to the publish, and I don't want to create an observer for each user and remove the observer handle dynamically, I'm pretty sure that will be really intense on the server, there must be a better way. Maybe setting up the observer in the client? but I don't want the client to have access to the log collection.

Products = new Mongo.Collection("products"); Log = new Mongo.Collection("log"); var initializing = true; Products.find().observe({ added: function(newDocument) { if (initializing) return; //how to get user here? Log.insert({ idUser: ???, date: new Date(), document: newDocument }); } }); initializing = false;

That's my first try, here's how I try it on the publish:

var productsObserveHandle; Meteor.publish('products', function() { var initializing = true; if (!productsObserveHandle) { productsObserveHandle = Products.find().observe( { added: function (document) { if (initializing) return; var userProfile = Meteor.users.findOne({_id:this.userId}).profile; Log.insert( { idUser: this.userId, userProfile: userProfile, date: new Date(), eventType: "added", document: document }); }.bind(this) }); } initializing = false; ... });

This one almost got it, but it always saves the log as the same user, no matter how many different users I try, since the observer handle is already created.

UPDATE: Ok, I think I'm getting somewhere, I set up the observe on the client after the collection subscription, and then all the observe methods added, changed, removed call a meteor method, that saves the log and gets the user by Meteor.userId() and it works, but now the deal is, that when you logout, and login, minimongo database gets updated, but the subscription is the same (I do all subscriptions on Meteor.startup, on client side), so I can't stop the observer and restart it after the update, is there a way to know when minimongo is being updated because of an account change? I could try to remove the observer after user logout and wait a timeout after login to setup the observer again, but I find this risky, since it may take more time to update minimongo than the timeout I setup and the last inserts may leak into the changes log.

最满意答案

这并不直接回答您的问题,而是提供替代解决方案。 我经常在我的meteor应用程序中实现这种模式,我总是使用Collection Hooks包。

更具体地说,您可以使用.after.insert(userId, doc)和.after.update(userId, doc, fieldNames, modifier, options)方法。 两者都正确处理用户ID。

如果您不想使用包,那么您应该能够看到它们如何解决您的问题,因为需要使包工作。

This doesn't directly answer your question, but provides an alternate solution instead. I have implemented this pattern often in my meteor apps and I always use the Collection Hooks package.

More specifically you could use .after.insert(userId, doc) and .after.update(userId, doc, fieldNames, modifier, options) methods. Both of which correctly handle the user id.

If you don't want to use a package for this, then you ought to be able to see how they solved your issue since it would be required to make the package work.

更多推荐