简体   繁体   English

使用流星方法传递用户数组

[英]Using Meteor methods to pass user array

I have this Meteor method on my server: 我的服务器上有以下Meteor方法:

returnUsers: function(){
        console.log(Meteor.users.find());
}

And the following call on my client: 以下是我的客户的电话:

'click #share_button': function(ev){
        ev.preventDefault();
        Meteor.call('returnUsers');
 }

But it returns an empty array: 但是它返回一个空数组:

LocalCollection.Cursor {collection: LocalCollection, sorter: null, _selectorId: undefined, matcher: Minimongo.Matcher, skip: undefined…}

How do I return an array of all users? 如何返回所有用户的数组?

You need to fetch the documents in order to return an array from your method. 您需要获取文档才能从您的方法返回数组。 Something like this: 像这样:

returnUsers: function() {
  var selector = {};
  var options = {fields: {username: 1}};
  return Meteor.users.find(selector, options).fetch();
}

Note that it's critical that you filter the fields in order to avoid sending all of your users' secrets to the client. 请注意,至关重要的是您过滤字段,以避免将所有用户的机密发送到客户端。 For more information see the "published secrets" section in my common mistakes article. 有关更多信息,请参见我的常见错误文章中的“已发布的机密”部分。

It's hard to say without knowing your use case, but instead of using a method, it may make more sense to publish some subset of users to your client, rather than fetching all of them on an event. 在不知道用例的情况下很难说,但是与其使用某种方法,不如某种用户子集发布给客户端而不是在事件中获取所有用户,这可能更有意义。


In order to get the result back from the server, you should invoke the method like this: 为了从服务器获取结果,您应该调用如下方法:

Meteor.call('returnUsers', function(err, users) {
  console.log(users);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM