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 166 forks source link

Roles.addUsersToRoles : MongoError: cannot use the part (roles of roles.GwbBhNhnNv5nAJANZN) to traverse the element ({roles: [ "admin" ]} #255

Closed lc3t35 closed 4 years ago

lc3t35 commented 6 years ago

First I've tryed the test of the package, everything is ok. Here is my code

const admins = Roles.getUsersInRole('admin').fetch();
  Meteor.users.find({}).forEach(function (user) {
    const group = "G".concat(user._id); // _id can start with a number, prefix with G letter
    console.log("process ", user.username, group);
    Roles.addUsersToRoles(admins, ['following'], group);
  });

If I remove group in last line, it iterates well on each users, when I add group, it stops silently at first iteration -> I've added a console.log(ex) at line 763 of alanning_roles to grab the suspected MongoError exception

{ [MongoError: cannot use the part (roles of roles.GwbBhNhnNv5nAJANZN) to traverse the element ({roles: [ "admin" ]})]
I20171031-15:43:13.690(1)?   name: 'MongoError',
I20171031-15:43:13.690(1)?   message: 'cannot use the part (roles of roles.GwbBhNhnNv5nAJANZN) to traverse the element ({roles: [ "admin" ]})',
I20171031-15:43:13.691(1)?   driver: true,
I20171031-15:43:13.691(1)?   index: 0,
I20171031-15:43:13.692(1)?   code: 16837,
I20171031-15:43:13.693(1)?   errmsg: 'cannot use the part (roles of roles.GwbBhNhnNv5nAJANZN) to traverse the element ({roles: [ "admin" ]})' }

Help would be greatly appreciated ;)

console.log(roles, group);
update = updateFactory(roles, group)
console.log(update);

outputs

[ 'following' ] 'GDqcy6TuqznNyFt9K7'
I20171031-15:54:23.120(1)? { '$addToSet': { 'roles.GDqcy6TuqznNyFt9K7': { '$each': [Object] } } }

is this similar to https://github.com/aldeed/node-simple-schema/issues/136 ?

mitar commented 4 years ago

The problem is that you started adding a role without groups and then start using groups. You cannot do this in v1, but you can in v2.