Meteor-Community-Packages / meteor-collection2

A Meteor package that extends Mongo.Collection to provide support for specifying a schema and then validating against that schema when inserting and updating.
https://packosphere.com/aldeed/collection2
MIT License
1.02k stars 108 forks source link

Meteor 1.3.2.4 user's collection no longer getting helpers? #330

Closed MichaelJCole closed 8 years ago

MichaelJCole commented 8 years ago

Edit

Sorry, this may be in the wrong place. I filed this other bug on collection-helpers

As a workaround, I did this:

    if (!Meteor.userId()) throw new Meteor.error('accessDenied', 'access denied');
    if (!Meteor.user()) throw new Meteor.error('accessDenied', 'access denied');
    var u = Meteor.users._transform(Meteor.user());
    if (!u.checkReputation('alwaysHappy')) throw new Meteor.error('accessDenied', 'access denied');
    ...

Original

Hi, I'm getting a regression with my user collection's helpers.

The user collection is defined essentially like this:

Schema.User = new SimpleSchema({
  // So many fields
});

Meteor.users.attachSchema(Schema.User);
Meteor.users.attachSchema(Schema.AutoTimestamps);

var userHelpers = {
  /* All the helpers */

  checkReputation: function(access) {
    check(access, String);
    if (!Meteor.userId()) throw new Meteor.Error('accessDenied', 'access denied');

    // Check to see if the user has enough reputation
    switch (access) {
      case 'alwaysHappy':
        return true;
      default:
        return false;
    }
  },
};
Meteor.users.helpers(userHelpers);

Meteor.users.before.update(function (userId, doc, fieldNames, modifier, options) {
  /* Update some stuff */
});

Meteor.users.after.update(function (userId, doc, fieldNames, modifier, options) {
  /* update some stuff */
});

And its used in a method like this;

  clientCreateOpportunity: function(op) {
    check(op, { /* details */});

    if (!Meteor.userId()) throw new Meteor.error('accessDenied', 'access denied');
    console.log(Meteor.userId());
    if (!Meteor.user()) throw new Meteor.error('accessDenied', 'access denied');
    console.log(Meteor.user());
    if (!Meteor.user().checkReputation('alwaysHappy')) throw new Meteor.error('accessDenied', 'access denied');
    /* Do more things */
  }

Meteor.user() used to have the helper function, and now it doesn't - in the last week or so.

I'm using Meteor 1.3.2.4 and these packages (summary):

aldeed:autoform                 5.8.1  Easily create forms with automatic insert and update, and automatic re...
aldeed:collection2              2.9.1  Automatic validation of insert and update operations on the client and...
aldeed:template-extension       4.0.0  Adds template features currently missing from the templating package
dburles:collection-helpers      1.0.4  Transform your collections with helpers that you define
iron:router                     1.0.12  Routing specifically designed for Meteor
matb33:collection-hooks         0.8.1  Extends Mongo.Collection with before/after hooks for insert/update/rem...
meteorhacks:cluster             1.6.9  Clustering solution for Meteor with load balancing and service discovery.
meteorhacks:kadira              2.28.5  Performance Monitoring for Meteor
meteorhacks:kadira-profiler     1.2.1  CPU Profiler for Meteor (used with Kadira)
ongoworks:security              2.0.1  Logical security for client-originated MongoDB collection operations
reywood:publish-composite       1.4.2  Publish a set of related documents from multiple collections with a re...
zimme:collection-behaviours     1.1.3  Define and attach behaviours to collections
zimme:collection-softremovable  1.0.5  Add soft remove to collections

What changed? Did I miss an update?

MichaelJCole commented 8 years ago

This was caused by Meteor.error() instead of Meteor.Error(). Meteor.error is undefined.