Javascript 检查allow()-函数中的角色,如果拒绝,则设置警告

Javascript 检查allow()-函数中的角色,如果拒绝,则设置警告,javascript,meteor,Javascript,Meteor,我正在使用alanning:roles,并希望检查给定的角色是否允许访问数据库。如果访问被拒绝(比如通知),是否可以执行一些代码 更新 正如你所看到的,我根本不理解抛出错误的方法 server.js Meteor.methods({ 'crashme':function() { throw new Meteor.Error(402, "Access Denied", "details", "more details"); } }); Users.allow({

我正在使用
alanning:roles
,并希望检查给定的角色是否允许访问数据库。如果访问被拒绝(比如通知),是否可以执行一些代码

更新

正如你所看到的,我根本不理解抛出错误的方法

server.js

Meteor.methods({
    'crashme':function() {
        throw new Meteor.Error(402, "Access Denied", "details", "more details");
    }
});

Users.allow({
    insert: function(userId) {
        if (Roles.userIsInRole(userId, 'admin')) return true;
    },
    update: function(userId) {
        if (Roles.userIsInRole(userId, 'admin')) return true;
    },
    remove: function(userId) {
        if (Roles.userIsInRole(userId, 'admin')) return true;
    }
});
Roles.addUsersToRoles(userId, 'anything');

Meteor.call("crashme", function(err, result) {
    console.log(err);
});
在客户端,我是这样做的

client.js

Meteor.methods({
    'crashme':function() {
        throw new Meteor.Error(402, "Access Denied", "details", "more details");
    }
});

Users.allow({
    insert: function(userId) {
        if (Roles.userIsInRole(userId, 'admin')) return true;
    },
    update: function(userId) {
        if (Roles.userIsInRole(userId, 'admin')) return true;
    },
    remove: function(userId) {
        if (Roles.userIsInRole(userId, 'admin')) return true;
    }
});
Roles.addUsersToRoles(userId, 'anything');

Meteor.call("crashme", function(err, result) {
    console.log(err);
});

如果addUsersToRoles失败,用户应该会看到一个通知…

在服务器端抛出meteor异常-在客户端捕获异常

为您的异常提供一条消息-将其显示给用户

throw new Meteor.Error(XX,e.message);
见下文:

试着这样做:

Users.allow({
    insert: function(userId) {
        if (Roles.userIsInRole(userId, 'admin')) 
           return true 
        else 
           throw new Meteor.Error(xx,xx);
    }
});
论客户

SomeCollection.remove(someId, function(err, result) {
  console.log(err.message); // Outputs "Not the owner"
});

也请看这篇文章:

是的,这应该有用。当然,这将显示在服务器控制台上,而不是客户端上。但是在客户机上进行检查也很容易,因为插入或更新会失败,并在回调中出错。嗯。。。不确定,如果我理解你的话。我想在客户端给用户一个通知信息。但我不知道该把密码放在哪里。上面的代码当然在服务器端…好的,现在我明白你的问题了。请看下面的答案。这听起来很合乎逻辑。你能举个例子吗?请看我文章中的链接。阅读链接和文档,但不幸的是,我不完全理解处理方法。更新了我的帖子,所以你会看到我的问题。