Hongbo Miao
Hongbo Miao

Reputation: 49984

How can I get every user info in code in meteor

How can I get access to every meteor user object? I tried this, but it shows Meteor.users does not have function forEach.

Meteor.users.forEach((user) => {
    console.log("userId", user._id);
});

Then I tried this, but it says userId is undefined.

_.toArray(Meteor.users).forEach((user) => {
    console.log("userId", user._id);
});

So how can I get it? Thanks

Upvotes: 2

Views: 131

Answers (1)

rishat
rishat

Reputation: 8376

Meteor.users is a Mongo collection. Mongo collections provide the map method to iterate through all found elements, but first, you have to find them. If you want to map through all the users without exception, just map without arguments, like this:

Meteor.users.find().map(user => console.log(user));

There, user will be an object that represents a user, i.e. similar to what you retrieve with Meteor.user().

Another way to iterate through all users would be to first fetch them in an instance of Array and then apply lodash or underscore to it:

const users = Meteor.users.find().fetch();
_.map(users, user => console.log(user));

Upvotes: 3

Related Questions