Parse JavaScript SDK and Promise Chaining

I have created a Background Job in Parse Cloud Code that sends out email notifications based on a date in one of my Parse classes.

Here is the idea: Query the class that contains the date. Iterate over each object returned and check the date field. If the date is equal to today, send out an email notification, change the date to null and save it back to Parse.

  • How do I call a cloud code function with parameters using the Javascript SDK
  • How to install Parse CloudCode in windows7 32bit?
  • Parse Cloud Code callback not working
  • Parse updating a set of objects
  • Parse request.object.get(“KEY”) always returns undefined
  • Result: v8 request size exceeds limits. Max allowed 1048576 current request is 1309246
  • However, it seems that not all the objects are saved back to Parse. I suspect this an issue with my promise chains, but I am having a hard time diagnosing the exact issue or how to fix it. Below is relevant code

    Parse.Cloud.job("job", function(request, status) {
    
    
    // Query for all users
    var query = new Parse.Query(className);
    query.each(function(object) {
    
        if (condition) {
    
            object.set(key, false);
            object.save(null, {
                success:function(object){
                    // This never executes!
                },
                error: function(error){
    
                }
    
            }).then(function(){
                // This never executes
                console.log("Save objects successful");
            },
            function(error){
                status.error("Uh oh, something went wrong saving object");
            });
    
            // Set hiatus end successful
            Mailgun.sendEmail({
            });
        }
    
        });
    });
    

    This line console.log("Save objects successful"); in the objects.save() promise chain does not ever get executed – even when the subscription object is successfully saved to Parse.

    Additionally, if there are more than 5 objects returned by the query, only the first 5 are successfully saved back to Parse. Any additional saves are not executed and email notifications are not sent.

  • How to access Facebook Graph API in Parse Cloud Code?
  • Parse cloud code - Removing a pointer from array of pointers
  • Parse cloud query to get object with closest GeoPoint
  • CloudCode multiple query in query doesn't work
  • Result: v8 request size exceeds limits. Max allowed 1048576 current request is 1309246
  • Parse Cloud Code callback not working
  • One Solution collect form web for “Parse JavaScript SDK and Promise Chaining”

    I’d clean it up as follows, relying on Promise.when …

    var savePromises = [];  // this will collect save promises 
    var emailPromises = [];  // this will collect email promises 
    
    // your code to setup the query here
    // notice that this uses find() here, not each()
    query.find(function(subscriptions) {
        _.each(subscriptions, function(subscription) { // or a for loop, if you don't use underscore
    
            // modify each subscription, then
            savePromises.push(subscription.save());
    
            // prepare each email then
            var emailPromise = Mailgun.sendEmail({ /* your email params object here */ });
    
            emailPromises.push(emailPromise);
        });
        // now do the saves
        return Parse.Promise.when(savePromises);
    }).then(function() {
        // now do the emails
        return Parse.Promise.when(emailPromises);
    }).then(function() {
        // Set the job's success status
        status.success("Subscriptions successfully fetched");
    
    // and so on with your code
    

    You might also consider combining the saves and the emails into one big array of promises, but it might be better to do it in two batches serially since they have different failure modes.