javascript 在 Meteor 中显示所有用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30961418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:01:10  来源:igfitidea点击:

Displaying all users in Meteor

javascriptmeteormeteor-accounts

提问by Matt

I have a template that I am trying to display all users in called userList.

我有一个模板,我试图在名为 userList 的情况下显示所有用户。

//server

//服务器

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

this.stop();
return;
});

Thanks in advance for the help!

在此先感谢您的帮助!

回答by JuanCrg90

if you want show all the user you can try in your publish.js file:

如果你想显示所有用户,你可以在你的 publish.js 文件中尝试:

Meteor.publish('userList', function (){ 
  return Meteor.users.find({});
});

in your router you susbcribe to this

在你的路由器中你订阅了这个

Router.route('/users', {
    name: 'usersTemplate',
    waitOn: function() {
        return Meteor.subscribe('userList');
    },
    data: function() {
        return Meteor.users.find({});       
    }
 });

The next step is iterate your data in the template.

下一步是在模板中迭代您的数据。

if you don't want subscribe in the router, you can subscribe in template level, please read this article for more details.

如果您不想在路由器中订阅,您可以在模板级别订阅,详细信息请阅读本文。

https://www.discovermeteor.com/blog/template-level-subscriptions/

https://www.discovermeteor.com/blog/template-level-subscriptions/

Regards.

问候。

回答by Hafiz ally lalani

This should work!

这应该有效!

// in server

// 在服务器中

    Meteor.publish("userList", function () {
           return Meteor.users.find({}, {fields: {emails: 1, profile: 1}});
    });

// in client

// 在客户端

    Meteor.subscribe("userList");

回答by Farid Blaster

This should work.

这应该有效。

  1. subscribe(client)
  2. publish(server)
  1. 订阅(客户)
  2. 发布(服务器)

Client:

客户:

UserListCtrl = RouterController.extend({
    template: 'UserList',
    subscriptions: function () {
       return Meteor.subscribe('users.list', { summary: true });  
    },
    data: function () {
       return Meteor.users.find({});
    }
});

Server:

服务器:

Meteor.publish('users.list', function (options) {
    check(arguments, Match.Any);
    var criteria = {}, projection= {};
    if(options.summary){
       _.extend(projection, {fields: {emails: 1, profile: 1}});
    }
    return Meteor.users.find(criteria, projection);
});