Meteor-Community-Packages / meteor-roles

Authorization package for Meteor, compatible with built-in accounts packages
http://meteor-community-packages.github.io/meteor-roles/
MIT License
921 stars 167 forks source link

Can't append to array using string field name #226

Closed michalmielczynski closed 7 years ago

michalmielczynski commented 7 years ago

Hey everyone,

I'm not sure if it's my fault or some meteor-roles incompatibility with new Meteor/Mongo.

While trying to execute: Roles.addUsersToRoles(Meteor.userId(), 'owner', 'example.org');

The last part (group parameter) causes an error (it works when last parameter is not given):

"Meteor.Error" message: "MinimongoError: can't append to array using string field name [example_org] [409]"

Any idea what's wrong? Thanks in advance!

michalmielczynski commented 7 years ago

Bit of debugging - seems that error is thrown in allaning_roles.js package at block 745 - 767:

 try {
      if (Meteor.isClient) {
        // On client, iterate over each user to fulfill Meteor's 
        // 'one update per ID' policy
        _.each(users, function (user) {
          Meteor.users.update({_id: user}, update)
        })
      } else {
        // On the server we can use MongoDB's $in operator for 
        // better performance
        Meteor.users.update(
          {_id: {$in: users}},
          update,
          {multi: true})
      }
    }
    catch (ex) {
      if (ex.name === 'MongoError' && isMongoMixError(ex.err)) {
        throw new Error (mixingGroupAndNonGroupErrorMsg)
      }

      throw ex
    }
alanning commented 7 years ago

I would guess that you were originally using the non-group version for that user? If you take a look at this section of the readme you can see the difference in database record structure depending on how you first set roles on that user:

https://github.com/alanning/meteor-roles#roles-changes

Have a look at the user's record and see if it is just a straight array. If so, you'll need to convert it to the other, object, format before using the "group" style of api calls.

michalmielczynski commented 7 years ago

Gosh, of course. I was dropping roles collection, thinking that would reset groups behavior - but clearly the problem lays in user.roles structure.

Thanks for solution! I was able to clean up unwanted straight arrays, now everything works as expected. Perhaps some wrapper to avoid storing 'non-group' roles will be time saver, but I guess this is something I can take care on my project side.

Thanks again!

alanning commented 7 years ago

Glad you got it working! Just to follow up, please have a look at the v2 branch as it has a new structure which avoids this issue and also allows indexing of roles data, which improves performance of some types of queries.

michalmielczynski commented 7 years ago

Thanks for pointing that out. I know it's probably out of topic, but... how would I install 2.0? (I got current version simply by adding alanning:roles in meteor/packages file)

alanning commented 7 years ago

To use v2.0, you'd download the repo to your local meteor /packages directory. Meteor will use the local version before using any others. So for example in our app, the resulting directory would look like:

app/packages/roles

On Wed, Jan 25, 2017 at 5:07 AM, supermelon notifications@github.com wrote:

Thanks for pointing that out. I know it's probably out of topic, but... how would I install 2.0? (I got current version simply by adding alanning:roles in meteor/packages file)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alanning/meteor-roles/issues/226#issuecomment-275068013, or mute the thread https://github.com/notifications/unsubscribe-auth/AAtvDKeM9OLye1S1y3UEaSGg0bhhGO5uks5rVx7hgaJpZM4LsU5B .