Search code examples
meteoruser-roles

Meteor alanning:roles - Error invoking Method 'updateRoles': Internal server error [500]


I am trying to set the role for the logged in user (using alanning:roles package) via a method on the server. Here's what I have...

Client

var userId = Meteor.userId();
Meteor.call('updateRoles',userId,'admin');

And this is the simplified version of the method from the docs...

server/userMethods.js

Meteor.methods({
    updateRoles: function (targetUserId, roles) {
        Roles.setUserRoles(targetUserId, roles)
    }
})

No matter what I try I keep getting the following error...

Error invoking Method 'updateRoles': Internal server error [500]

Solution

  • Problem solved.

    The reason was because I am using autoform (simple schema) for the 'users' collection and I needed to include the following (uncommented part) in the schema...

    // Add `roles` to your schema if you use the meteor-roles package.
    // Option 1: Object type
    // If you specify that type as Object, you must also specify the
    // `Roles.GLOBAL_GROUP` group whenever you add a user to a role.
    // Example:
    // Roles.addUsersToRoles(userId, ["admin"], Roles.GLOBAL_GROUP);
    // You can't mix and match adding with and without a group since
    // you will fail validation in some cases.
    //
    //roles: {
    //    type: Object,
    //    optional: true,
    //    blackbox: true
    //},
    // Option 2: [String] type
    // If you are sure you will never need to use role groups, then
    // you can specify [String] as the type
    
    roles: {
        type: [String],
        optional: true
    },