Issue in using query.distinct in react with useMoralisQuery hook

Hello, I have a table in moralis server with columns “flip”, “bet”, “userName”, “totalAmount”, "
createdAt". Now, I want to get the live results(basically only 2 rows(results) satisfying conditions in query) for users who has names as “x”, “y” and bet amount equal to 100 for each and latest values for this conditions. i.e user “x” has bet amount 100 and its just 10 secs ago. same for “y”. So, I created table in frontend to show only this two users latest details with those conditions using useMoralisQuery shown below.

const { data , isFetching } = useMoralisQuery(
        (query) => {
                   .equalTo("bet", 100)
                   .limit(2) // need only x & y users for now
                   .then(function(results) {
                       // my logic
                  .catch(function(error) {
                    console.log("error =", error)
               return query
            live: true,
            // onLiveEnter: (entity, all) => [...all, entity],
            onLiveCreate: (entity, all) => [entity, ...all],
            onLiveDelete: (entity, all) => all.filter((e) => !==,
            // onLiveLeave: (entity, all) => all.filter((e) => !==,
            onLiveUpdate: (entity, all) =>
       => ( === ? entity : e)),

Im getting this error:

Unhandled Rejection (Error): You need to call Parse.initialize before using Parse

What am I doing wrong? How to change above query to get result for only “x”, “y” users for entries for bet=100 and latest values of both with that conditions from table?

I could not find any working examples in documents wrt react hook with distinct query. Please help me resolve this. Any links or working examples for distinct in react would be helpful.

Thanks in advance.

did you try to make a cloud function that makes that query?

that error with Parse.initialize it looks like Moralis is not yet initialised when you try to run that query, you can test if it is initialised with:

const { Moralis, isInitialized, } = useMoralis();
You will have access to the following values by using this hook:

Option	Description
Moralis	The global Moralis instance (same as the global Moralis object)
isInitialized	A boolean, indicating if Moralis has been initialized
isInitializing	A boolean, indicating if Moralis is currently initializing

@cryptokid Thanks for the reply.

For trying that query on backend puts lot of overhead for server, so I want to make it work on frontend side only as it will be live query for which frontend will update accordingly.

I tried with isInitialized for query, if isInitialized is true then only Im returning query inside useMoralisQuery, so parsing error is not there anymore, but now Im getting new error as this

error: "unauthorized: master key is required"

How to use masterkey with my query?
Even document also says that below

But how can we use “masterkey” with query at frontendside is not given.
Can you help me in that?

you shouldn’t use master key in front end, because you can do any read or write in database with the master key

you can use master key in a cloud function or in a backend

@cryptokid Got it. so you are saying I cannot use query.distinct at frontend side as master key is needed and masterkey should be used at backend side only. So, there is no way to achieve my query conditions at frontend side, I have to write cloud function to get the results as per my query?

I don’t know exactly now about that master key requirement for using .distinct in general, for that particular case when querying User class, than you need master key because you can not query the data for all the users without master key

@cryptokid Ohh ok. These users are just a part of table data simply as a key and nothing more than that. I have other tables where Im getting the data as per my query conditions. So, I’m not sure why masterkey is required just to query data based on mentioned conditions in query at frontend side. I simply want to get users “x” and “y” not other users “y”, “z”,…etc and who have amount 100 recently. Is there any way to get that data from frontend apart from using query.distinct ?

you can make a cloud function that queries the data with .distinct and then you can call that cloud function from front end. You can also use parameters when you call that cloud function

1 Like

@cryptokid yes it seems that the only way to use distinct in query at backend side rather than frontend. Thanks for helping. Appreciate it.

1 Like