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.
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.