meteor Roles.userIsInRole() always returning false

六月ゝ 毕业季﹏ 提交于 2019-12-25 07:29:11

问题


my first button creates a simple user like this:

'click .insertBtn': function()
    {
        var email = document.getElementById('email').value;
        var pass = document.getElementById('password').value;

        Accounts.createUser(
        {
            email: email,
            password: pass,
            profile: { name: 'test' }
        }); 

        Roles.addUsersToRoles([this._id], ['admin']);
    }

This user gets assigned a role of "admin". I then while I am logged in have a button to check if I am admin:

'click .checkRole': function()
    {
        console.log(Roles.userIsInRole(Meteor.userId(), 'admin'));
    }

But it is always returning false, so what may be the problem?

The id of the user and the role are different, is that how it should be?


回答1:


The user may not have successfully been added to the role. The thing is you really use this line of code on the client side (Because then anyone would just type that in to themselves and become admin):

Roles.addUsersToRoles([this._id], ['admin']);

You have to do this process on the server, perhaps in the Accounts.onCreateUser callback on the server side of your project.

Or you could connect to your meteor's mongodb and add a roles: ['admin'] field to your user's document in the users collection if this is something once off.



来源:https://stackoverflow.com/questions/23250668/meteor-roles-userisinrole-always-returning-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!