用户集合中所有用户的列表第一次不使用流星 js [英] Listing of all users in the users collection not working first time with meteor js

查看:20
本文介绍了用户集合中所有用户的列表第一次不使用流星 js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列出用户集合中的所有用户时遇到问题.当我进入列表页面时,只显示当前登录用户的详细信息.但是,一旦页面刷新并且一切正常,所有用户都会被列出.

I am having the issue with listing all the user's in the users collection. When I take the listing page, only the currently logged in user's details are shown. But all users are getting listed once the page is refreshed and there on its fine.

在服务器端,我有以下发布代码

On Server side, I have the following publish code

Meteor.publish("userList", function() {

    var user = Meteor.users.findOne({
        _id: this.userId
    });


    if (Roles.userIsInRole(user, ["admin"])) {
        return Meteor.users.find({}, {
            fields: {
                profile_name: 1,
                emails: 1,
                roles: 1,
                contact_info: 1
            }
        });
    }

    this.stop();
    return;
});

在客户端,

Meteor.subscribe('userList');

在Template js文件中,我进行了如下调用,

In the Template js file, I make the following call,

Meteor.users.find();

请帮我解决这个问题.我在这里错过了什么?

Please help me out with this issue. What am I missing here ?

推荐答案

这听起来像是订阅的竞争条件(它在用户登录之前运行).我建议将您的订阅放在 autorun 中:

It sounds like a race condition with the subscription (it runs before the user is logged in). I'd recommend putting your subscription inside of an autorun:

Tracker.autorun(function() {
  if (Meteor.user()) {
    Meteor.subscribe('userList');
  }
});

这有一个额外的好处,即在用户登录之前不开始订阅(节省资源).

This has the additional benefit of not starting your subscription before the user is logged in (saves resources).

顺便说一句,我想不出为什么你需要 this.stop() 和发布函数的结尾.

BTW, I can't think of a reason why you'd need the this.stop() and the end of your publish function.

这篇关于用户集合中所有用户的列表第一次不使用流星 js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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