[SOLVED] Is there a way to track how many users in the Moralis database?

Hi Moralis community! I’m currently working on my hackathon project and was wondering if there is a way to list the total number of users in the app in the front-end.

For example, perhaps it’s an API call that routes to the users in the database and updates as more users get added to the platform. Was really thinking it would be just a home-screen thing.

Is this something that Moralis supports right now?

you can make a cloud function where you can do something like this:

Moralis.Cloud.define("get_nr_users", async (request) => {
  query = new Moralis.Query("User");
  x = await query.count({useMasterKey : true});
  return x;
 }); 

and then call that function in front end with Moralis.Cloud.run('get_nr_users')

1 Like

Thanks @cryptokid! In order to call this from the front-end, there is something wrong with my template literal: ${Moralis.Cloud.run('get_nr_users')} is there a reason why this isn’t working?

I’m not familiar with that syntax, I don’t know exactly what is the problem, it could be the part with 'get_nr_users' maybe.

If you run it from browser console it works fine?
You will also have to use await in that syntax: nr_users = await Moralis.Cloud.run('get_nr_users') works for me

1 Like

Thanks so much!! Figured out React has a great way to run this via a useEffect hook and then call the output in the front-end and it works perfectly :blush:

1 Like

@cryptokid is that a way to approach most effectively fetching users online count by say a timestamp threshold?