[SOLVED] How to get all the users in Moralis server using a cloud function

I have write a cloud function to get all the entities in โ€˜Userโ€™ Object.

Moralis.Cloud.define("getUsers", async (request) => {
    const query = new Moralis.Query("_User", { useMasterKey: true });
    return query;
});

User object is not empty but here is the response for the above cloud function. I have used Calling via REST API method to get the response.
Response

{
    "result": {
        "className": "_User",
        "_where": {},
        "_include": [],
        "_exclude": [],
        "_count": false,
        "_limit": -1,
        "_skip": 0,
        "_readPreference": null,
        "_includeReadPreference": null,
        "_subqueryReadPreference": null,
        "_queriesLocalDatastore": false,
        "_localDatastorePinName": null,
        "_extraOptions": {},
        "_xhrRequest": {
            "task": null
        }
    }
}

You may need to use .find

 Moralis.Cloud.define("getUsers", async (request) => {
    const query = new Moralis.Query("_User", { useMasterKey: true });
    const results = await query.find();
    return results;
});

Response

{
    "result": []
}

User Object in Moralis
image

You need to use master key for that find

useMasterKey: true

Is this not enough?

I donโ€™t know about this syntax. There is a syntax with master key when find is run

Thank you @cryptokid. I forgot to use useMasterKey: true for .find method. This cloud function works well.

Moralis.Cloud.define("getUsers", async (request) => {
    const query = new Moralis.Query("_User", { useMasterKey: true });
    const results = await query.find({ useMasterKey: true });
    return results;
});
1 Like

This is limited to 100. I only got 100 users. How can I get full list of all users?

you can use query.limit(200) to get 200 rows

1 Like