Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the same collection in a hook's callback can stop publication functions from firing #244

Open
evolross opened this issue Jul 18, 2018 · 0 comments

Comments

@evolross
Copy link
Contributor

Hit another nasty bug with collection hooks today in production. Wanted to leave it here in case I jump in and try to fix these.

My use case is inserting a document into an ordered list of documents that are paginated by pages of ten documents. They use an order field for the ordering.

If you have a simple collection hook like:

Documents.before.insert(function (userId, doc) {
   //  Increment the order of all existing documents after this document
   var order = doc.order;
   Documents.update({order: {$gte: order}}, {$inc: { order: 1}}, {multi: true});
});

And you do an insert:

let someDocument = {text: "someText", order: 3};
Documents.insert(someDocument);

And you have a publication (I've intentionally broken out the functions for debugging and am logging each one):

Meteor.publish('documentsByPage', function(limit, skip) {
   var self = this;

   var handle = Documents.find({}, {
      sort: {order: 1},
      limit: limit,
      skip: skip,
   }).observe({
      added: function (added) {
         self.added("questions", added._id, added);
      },
      changed: function(changed, old) {
         self.changed("questions", changed._id, changed);
      },
      removed: function(removed) {
         self.removed("questions", removed._id);
      }
   });

   self.ready();

   self.onStop(function () {
     handle.stop();
   });
});

For some reason calling an update on the same collection in its before.insert hook, in some cases, will actually totally prevent the original insert from firing an added call to the publication. It happens in both an old-school client insert and a Meteor Method insert. And it will happen no matter if I use the before.insert or after.insert hook. It seems like Meteor is "grouping" up the publication calls and having this update to the same collection in the hook causes issues.

For some weird reason it usually only happens when users are past the first page of pagination in my app. Everything works fine on the first page. As soon as I comment out that hook, the added call to the publication fires fine. With the hook, I only get the changed calls to the documents with a higher order number that get incremented.

So somehow that update is affecting the insert's added call to the publication. If I page back or refresh, the data is actually inserted, it just doesn't fire an added call. So my client looks like it failed. My work-around is to do the incrementing in a Meteor Method in the callback of the insert.

I will try to work on these (or further investigate using Node's events versus this package).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant