2

I am working on a project using Parse where I need some information calculated for each user and updated when they update their account. I created a Cloud Code trigger that does what I need whenever a user account is updated, and that is working well. However, I have about two thousand accounts that are already created that I need to update as well. After hours of trying to get a Cloud Job to work, I decided to try to simplify it. I wrote the following job to simply count the user accounts. To reiterate; I'm not actually trying to count the users, there are much more efficient ways to do that, I am trying to verify that I can query and loop over the existing user accounts. (The option to useMasterKey is in there because I will need that later.)

Parse.Cloud.job("getUserStatistics", function(request, status) {
    // Set up to modify user data
    Parse.Cloud.useMasterKey();
    // Query for all users
    var query = new Parse.Query(Parse.User);
    var counter = 0;
    query.each(function(user) {
        counter = counter+1;
    }).then(function() {
        // Set the job's success status
        status.success("Counted all User Accounts.");
    }, function(error) {
        // Set the job's error status
        status.error("Failed to Count User Accounts.");
    });
    console.log('Found '+counter+' users.');
});

When I run the code, I get:

I2015-07-09T17:29:10.880Z]Found 0 users.
I2015-07-09T17:29:12.863Z]v99: Ran job getUserStatistics with:
  Input: "{}"
  Result: Counted all User Accounts.

Even more baffling to me, if I add:

query.limit(10);

...the query itself actually fails! (I would expect it to count 10 users.)

That said, if there is a simpler way to trigger an update on all the users in a Parse application, I'd love to hear it!

1 Answer 1

3
  1. The reference actually says that:

The query may not have any sort order, and may not use limit or skip.

https://parse.com/docs/js/api/symbols/Parse.Query.html#each

So forget about "query.limit(10)", that's not relevant here.

  1. Anyways, by their example for a background job, it seems you might have forgotten to put return in your "each" function. Plus, you called console.log('Found '+counter+' users.'); out side of your asynchronous task, that makes sense why you get 0 results. maybe you try:

    query.each(function(user) {
        counter = counter+1;
        // you'll want to save your changes for each user,
        // therefore, you will need this
        return user.save();
    }).then(function() {
        // Set the job's success status
        status.success("Counted all User Accounts.");
        // console.log inside the asynchronous scope
        console.log('Found '+counter+' users.');
    }, function(error) {
        // Set the job's error status
        status.error("Failed to Count User Accounts.");
    });
    

You can check again Parse's example of writing this cloud job.

https://parse.com/docs/js/guide#cloud-code-advanced-writing-a-background-job

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I was able to get it fixed. Also found that I could only update 8 records at a time, but I was able to make it a job and run it every minute for six hours.
I'm glad it worked for you. I wonder if you could share what type of cloud job you are planning for each user. best regards.
I'm making a user management interface, and realized that users who have Facebook or Twitter accounts have really weird user names. I wrote a trigger that pulls the user accounts and saves some basic information about them in another part of the user record so that it's easy to query against. The cloud job is updating the some 2k users already in the users table so that I can tell who has Facebook and Twitter accounts.
Oh, that really sounds fascinating, I wish you a lot of success with your app :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.