percolatestudio / publish-counts

Meteor package to help you publish the count of a cursor, in real time
https://atmospherejs.com/tmeasday/publish-counts
MIT License
200 stars 46 forks source link

TypeError: self.added is not a function #90

Closed anthonyboutinov closed 8 years ago

anthonyboutinov commented 8 years ago

I'm trying to publish events.count:

  Meteor.publish('events', (categoryIds, options) => {
    Counts.publish(this, 'events.count', Events.find({categoryId: {$in: categoryIds}}), {noReady: true});
    return Events.find({categoryId: {$in: categoryIds}}, options);
  });

I get an exception:

Exception from sub events id X7tNSJuqu2zhHW8i4 TypeError: self.added is not a function
at Object.Counts.publish (packages/tmeasday_publish-counts/server/publish-counts.js:79:1)
at Subscription._handler (imports/api/events.js:10:12)
at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1711:12)
at Subscription._runHandler (packages/ddp-server/livedata_server.js:1026:17)
at Session._startSubscription (packages/ddp-server/livedata_server.js:845:9)
at Session.sub (packages/ddp-server/livedata_server.js:617:12)
at packages/ddp-server/livedata_server.js:551:43

METEOR@1.4.1.2 tmeasday:publish-counts@0.8.0

dburles commented 8 years ago

@anthonyboutinov you'll want to avoid using the ES6 arrow function syntax here as this will be bound to outer function scope. In other words:

Meteor.publish('events', (categoryIds, options) => {

Should be:

Meteor.publish('events', function(categoryIds, options) { 
sladkovm commented 8 years ago

I think it would help to add the remark of not using ES6 to the README

dburles commented 8 years ago

@sladkovm it wouldn't really make sense to as it's Meteor's publish API that puts methods on the function context.

boxofrox commented 8 years ago

This issue is related to #84. I maintain the same stance as @dburles.

With any javascript project, one must always consider the consequence concerning the function context this when using ES6 arrow functions.

In general, the ES6 arrow function is not intended as a blind replacement for traditional ES5 function definitions, especially where callbacks are concerned, and furthermore when your callback utilizes this within the callback.

Meteor even documents [1] that Meteor.publish uses the callback's context.

Function called on the server each time a client subscribes. Inside the function, this is the publish handler object, described below. If the client passed arguments to subscribe, the function is called with the same arguments.

While I don't relish the idea of enumerating broad programming mistakes in the README, it may be worthwhile to use the FAQ label and apply it to these issues. At the very least, we can add a FAQ link to the README. The README remains idempotent to changing trends in programming mistakes, and users are presented with more direction to find these discussions. Thoughts, @dburles?

dburles commented 8 years ago

@boxofrox since Meteor is a platform that caters to all skill levels we might as well if it saves people a bit of a headache, the arrow function can be a bit of a gotcha for new developers (and those new to ES6).

boxofrox commented 8 years ago

I've added the a url for frequently asked questions to the README.