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

MongoError: Cannot apply $pull to a non-array value #282

Closed rich-m21 closed 4 years ago

rich-m21 commented 4 years ago

I'm trying to remove users in roles using removeUsersFromRoles but get the mongo error on title Meteor version 1.8.1 alanning:roles@1.2.16

            const currentRoles = Roles.getRolesForUser(data.userId);
            console.log(currentRoles);
            console.log(Array.isArray(currentRoles));
            console.log('==============Removing Past Roles==============');
            Roles.removeUsersFromRoles(data.userId, currentRoles);

currentRoles prints out

I20190911-23:49:34.788(-6)? [ 'app-ratings',
I20190911-23:49:34.794(-6)?   'visibility',
I20190911-23:49:34.800(-6)?   'manifest',
I20190911-23:49:34.802(-6)?   'statistics',
I20190911-23:49:34.812(-6)?   'healthcheck',
I20190911-23:49:34.814(-6)?   'live-poi',
I20190911-23:49:34.821(-6)?   'app-logs',
I20190911-23:49:34.822(-6)?   'super-admin',
I20190911-23:49:34.826(-6)?   'users-admin' ]

and isArray prints out true Help!

alanning commented 4 years ago

Can you post the "roles" field from that user's database record, pls? Generally that means that a "$pull" mongodb command is being attempted on a field that is not an array.

rich-m21 commented 4 years ago

Yes, I figured that much. Ended up doing the removal manually with an $unset directly to the field. It's really weird also cause I was passing in an array. { "_id" : "aMDsKA8TDFmHqxvpe", "createdAt" : ISODate("2019-09-12T16:46:07.096Z"), "services" : { "password" : { "bcrypt" : "$2a$10$uKIy536xc1cokpm7NW0nTuGnaFLdQcrH0gHUAcgifVY8SwiLFC66e" } }, "username" : "TestRoleA", "emails" : [ { "address" : "richmerren@gmail.com", "verified" : false } ], "roles" : { "qa" : [ "visibility", "live" ] } }

alanning commented 4 years ago

I think the function documentation is misleading. Since that user record uses "groups", you need to also pass the target group to Roles.removeUsersFromRoles(data.userId, currentRoles, 'qa').

Not passing a group could indicate you want to remove the user from those roles in any group but that's not the way the code works today since it still supports library users who don't use groups at all: https://github.com/alanning/meteor-roles/blob/master/roles/roles_common.js#L227-L232

mitar commented 4 years ago

I think I just published a package where you should get a better error message in this case.