Javier Mendonça
Javier Mendonça

Reputation: 2060

Using Parse objects in CloudCode private function

I am creating a new CloudCode function in parse to retrieve the total number of users in the database. I managed to achieve this by doing this:

    Parse.Cloud.define("getUsers", function(request, response) {
    var users = Parse.Object.extend("_User");
    var query = new Parse.Query(users);

    query.find({
        success: function(numberOfUsers) {
        response.success(numberOfUsers.length);
  },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message);
    }
});
});

Now, I want to use the total number of users to set a parameter in my Parse User. I thought defining a private function (pretty much the same as the one shown above) that returns that number so I can call it in my other cloud function, like this:

Parse.Cloud.define("signUp", function(request, response) {
    var user = new Parse.User();
    user.set("username", request.params.username);
    user.set("password", request.params.password);

    //setting empty fields
    ...
    user.set("alias", "user#" + numberOfUsers());

    user.signUp(null, {
    success: function(user) {
        response.success(user);
    },
    error: function(user, error) {
        alert("Error: " + error.code + " " + error.message);
  }
});
});

function numberOfUsers() {
    var users = Parse.Object.extend("_User");
    var query = new Parse.Query(users);

    query.find({
        success: function(numberOfUsers) {
        return numberOfUsers.length;
  },
    error: function(error) {
        return 0;
    }
});
}

but this doesn't work for some reason. The function numberOfUsers() returns empty. I see some other people have problems with using parse objects in private functions as well, I found the same question in the Parse dashboard without answer... https://parse.com/questions/how-to-define-helper-method-in-cloud-code Anyone knows how to achieve this? Thank you!

Upvotes: 1

Views: 160

Answers (1)

Björn Kaiser
Björn Kaiser

Reputation: 9912

The issue here is that query.find runs asynchronously so it returns immediately even though the query is not finished yet. Also keep in mind, that the maximum limit you can set on a query is 1000 objects.

So once your user-base grows above this number you will have to page through the results which can take n amount of time. Which can become a problem later down the road as Cloud Code functions are limited to run for 15 seconds max.

Instead of counting your users each time you could keep track of the number of users by increasing a counter column on an object each time a user is created and decrease it once a user object will be deleted.

You can use afterSave and afterDelete hooks here, update the counter column and in your getUsers function you simply return the value of that column.

Now you need to get around the sync/async problem, here you can use JavaScript Promises to chain the different operations and make sure they run in a series.

Upvotes: 2

Related Questions