流星 Roles.userIsInRole() 总是返回 false [英] meteor Roles.userIsInRole() always returning false

查看:32
本文介绍了流星 Roles.userIsInRole() 总是返回 false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的第一个按钮创建了一个像这样的简单用户:

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'));
    }

但它总是返回false,那么可能是什么问题?

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

用户和角色的id不一样,是不是应该这样?

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

推荐答案

用户可能没有成功添加到角色中.问题是你真的在客户端使用这行代码(因为这样任何人都会自己输入并成为管理员):

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']);

您必须在服务器上执行此过程,可能在 Accounts.onCreateUser 回调中项目的服务器端.

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

或者您可以连接到您的meteor 的mongodb 并添加一个roles: ['admin'] 字段到您在users 集合中的用户文档(如果这是一次性的).

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.

这篇关于流星 Roles.userIsInRole() 总是返回 false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆