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
920 stars 164 forks source link

E11000 duplicate key error collection #307

Closed DeanKamali closed 4 years ago

DeanKamali commented 4 years ago

I'm trying to create a number of new roles and I keep getting this error

> Roles.createRole('admin');
MongoError: E11000 duplicate key error collection: meteor.roles index: name_1 dup key: { name: null }

> Roles.createRole('provider');
MongoError: E11000 duplicate key error collection: meteor.roles index: name_1 dup key: { name: null }

alanning:roles@3.1.0

Any ideas?

SimonSimCity commented 4 years ago

Does this happened after updating your roles collection from 1.x, or at least you still have the index on the name field still on your roles collection?

DeanKamali commented 4 years ago

Yes, it started to happen after the upgrade, I'm not sure what version I had before 1.x or 2.x checking git, it only says alanning:roles from before.

I only have 3 roles (admin, patient, provider) in roles collection

/** Meteor shell **/

> Roles.getAllRoles().fetch()
[ { _id: 'aWJYaTEjbLzYCJTdu', name: 'admin' },
  { _id: 'owAd3tYj5ZtKWCLTn', name: 'patient' },
  { _id: 'ssYDT95Gcv2xJvKBs', name: 'provider' } ]

> Roles.addUsersToRoles('q5jdXMSiTavQqQkgn', 'admin'); // Trying to add a user to role
Error: Role 'admin' does not exist.
    at evalCommandPromise.then (packages/shell-server/shell-server.js:249:21)
    at runBound (domain.js:314:12)
    at bound (domain.js:301:14)
    at defaultEval (repl.js:240:29)
    at ContextifyScript.Script.runInThisContext (vm.js:50:33)
    at repl:1:-55
    at Object.addUsersToRoles (packages/alanning:roles/roles/roles_common.js:372:11)
    at Array.forEach (<anonymous>)
    at packages/alanning:roles/roles/roles_common.js:379:13
    at Array.forEach (<anonymous>)
    at packages/alanning:roles/roles/roles_common.js:380:15
    at Object._addUserToRole (packages/alanning:roles/roles/roles_common.js:473:15)
> 
SimonSimCity commented 4 years ago

The roles you show me also are according to the first version of this package. Please run the update script before running any additional commands. First update to v2.0 and then to v3.0. I advise you to do it in two separate steps as proposed in the readme (https://github.com/Meteor-Community-Packages/meteor-roles#migration-to-30).

Please keep in mind that the update to version 1.x will still leave the old roles in place, which you have to remove manually until https://github.com/Meteor-Community-Packages/meteor-roles/issues/300 is resolved.

SimonSimCity commented 4 years ago

Let me know if there's anything else, but this should solve it. Please ask to reopen if it doesn't.

DeanKamali commented 4 years ago

Well, here is what I ended up doing, I dropped roles and role-assignment and it got re-created when I added new roles.

Things started to work, however, I faced an issue with getUsersInRole returning an empty array all the time.

Turned out that my local MongoDB use newer version 4.2 and by downgrading to version 4.0.10 getUsersInRole method started to work.

SimonSimCity commented 4 years ago

Oh! Thanks for notifying! Could you create a new issue for this?

jamauro commented 2 years ago

If anyone else comes across the same problem, the sequence of upgrading is important.

  1. Type meteor remove alanning:roles and then meteor add alanning:roles@2.0.0
  2. Run the migration script in the shell https://github.com/Meteor-Community-Packages/meteor-roles#migration-to-30
  3. Then update your code e.g. add things like Roles.createRole. (If you do this step before the migration script you'll get the duplicate error key.)