How can i list all the users on the client side

As Meteor.users is working only on server side and displaying all the users details but when applying same code Meteor.users to client side, it is displaying only the logged in user. How to display all the users on client. Please someone help me on this. Even publish and subscribe is not working here. Can anyone help.

You can write a Meteor method that returns information about all users.

Client:

Meteor.call('getUsers', { filter }, (err, res) => { // filter would be an object like: { filter:  { gender: 'f', age: { $gt: 25 } } }
      if (err) {
        console.log(err)
      } else {
        console.log(res) // res is your users
      }
    })

Server:

const getUsers = new ValidatedMethod({
  name: 'getUsers',
  validate: new SimpleSchema({
    filter: { type: Object, blackbox: true, optional: true } // filter to apply to query
  }).validator(),
  run ({ filter }) {
    if (Meteor.isServer) {
      if (!this.userId) { return false } // if you want to restrict this method to only logged in users

      const options = {
        fields: {
          // .... fields that you want to return per each user
          // .... leave empty or don't even mention options for all fields
        }
      }

      if (this.userId === 'your user id') { // only your user can list all users
        return Meteor.users.find({}, options).fetch()
        // or return Meteor.users.find(filter, options).fetch()
        // you send the filter from your client like { gender: 'f', age: { $gt: 25 } }
      }
      return null // if the user is not you, return a null or don't return anything, or return a false, empty array etc.
    }
  }
})

On the client side, only logged in user it is displaying, so in meteor.user only logged in user info is there .
How can I get name of all the users from the database.

@kumarrohan did you added fields to the options (referring to the example above)?

I don’t want to filter anything… I want to get al the fields info

If you leave the fields property out of the options, then you won’t filter anything at all. See the docs

validatemethod is not defined? What should I use instead of that?

validatemethod is not defined? What should I use instead of that?

Please read it here: https://guide.meteor.com/security.html#validated-method

In general I suggest you to read the full guide, it covers most topics and contains examples nearly everywhere. You can actually use the topics from the guide to learn the framework step by step. Many questions will be answered there.

1 Like

Thanks :slightly_smiling_face: