Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于登录用户的Meteor动态发布';s";角色“;_Meteor_Meteor Publications_Meteor Accounts - Fatal编程技术网

基于登录用户的Meteor动态发布';s";角色“;

基于登录用户的Meteor动态发布';s";角色“;,meteor,meteor-publications,meteor-accounts,Meteor,Meteor Publications,Meteor Accounts,我希望有些出版物只返回用户根据其角色可以访问的项目。我正在使用alanning:roles包来处理这些角色 例如,我有一本出版物,如: Meteor.publish('header_fields', function() { console.log("header_fields: %j", this.userId); if (Roles.userIsInRole(this.userId,['ADMIN','INSPECTOR'])) { // Inspectors

我希望有些出版物只返回用户根据其角色可以访问的项目。我正在使用alanning:roles包来处理这些角色

例如,我有一本出版物,如:

Meteor.publish('header_fields', function() {
    console.log("header_fields: %j", this.userId);
    if (Roles.userIsInRole(this.userId,['ADMIN','INSPECTOR'])) {
        // Inspectors and Admins can see all header fields
        return HeaderFields.find();
    } else {
        // Clients should only be able to see header fields for forms they have access to
        var user = Meteor.users.find({_id: this.userId});
        var formIds = [];
        _.each(Forms.find({client_id: user.profile.client_id}).fetch(), function(form) {
            this.push(form._id);
        }, formIds);
        return HeaderFields.find({form_id: {$in: formIds}});
    }
});

我遇到的问题是,在我看到的所有示例中,发布都是在服务器文件夹中的.js文件中定义的,因此在客户端首次连接时运行并获取。但是,当客户端最初连接时,用户没有登录。但是,我不知道这些出版物放在哪里,也不知道它应该如何工作。

Meteor默认以优雅的方式处理这个特定的用例,引用官方文档:

此.userId

访问发布函数内部。已登录用户的id,如果没有用户登录,则为null。这是不变的。 但是,如果登录的用户发生更改,则会重新运行发布功能 具有新的值


因此,基本上这意味着,如果您在客户端订阅了一个出版物,该出版物返回基于
this.userId
的不同文档,就像在您的示例中一样,一切都应该开箱即用

是的,我刚刚又读了一遍,看到了。它现在运行良好,我只是将每个出版物的全部内容包装在一个
if(this.userId)…
中。我的下一个困惑是,每次我点击一个链接,它都在运行发布,就好像用户在改变一样。我不确定这会不会发生。