0

new to parse in the last couple of days, and was wondering if anyone might know what my problem is. I'm trying to create a background job that retrieves a list of strings from an external server, then loops through that list, saving objects containing those strings with a could function. The server returns a list of 20 items, but only 9 objects are saved. Even odder, it seems that the function that would save the object isn't even being called for the i

Here is my code:

var MyObject = Parse.Object.extend("MyObject");

Parse.Cloud.job("getNew", function (request, status) {
    // Set up to modify user data
    Parse.Cloud.useMasterKey();

var newObjects;
Parse.Cloud.httpRequest({
    url: 'myURL',
    success: function (httpResponse) {
        newObjects = httpResponse.data;
        var arrayLength = newObject.length;
        console.log('got ' + newObjects.length + ' new Objects');
        for (var i = 0; i < arrayLength; i++) {   
            console.log('saving Object ' + newTopics[i]);
            Parse.Cloud.run('saveObject', {
                name: newObjects[i]
            }, {
                success: function (message) {
                    console.log(message);
                },
                error: function (error) {
                    console.log(error);
                }
            });


        }
        status.success("New Objects Added.");
    },


  error: function (httpResponse) {
        console.error('Request failed with response code ' + httpResponse.status);
        status.error('Request failed with response code ' + httpResponse.status);
    }


 });


});


Parse.Cloud.define("saveObject", function (request, response) {
    var query = new Parse.Query("Review");
    var name = request.params.name;
var object = new MyObject();
object.set("name", name);
object.set("nameLowerCase", name.toLowerCase());
console.log('saving the object ' + object.get("name"));
object.save(null).then(
    function (object) {
        response.success('New object created with objectId: ' + object.get("name"));
    },
    function (error) {
        console.log('Failed to create new object, with error code: ' + error.message);
    });
});

My log output for the job is showing that the saveObject function isn't even being called for the unsaved items, even though the console log statement on the line before the call to save object call for those items does get logged.

Thanks to anyone who reads this, I would really appreciate the help.

Mörre
  • 5,699
  • 6
  • 38
  • 63
  • I've found a way to save all of the items using Parse.Object.saveAll(). I still don't understand my the previous approach didn't work, but maybe I should just use this as the answer. – SaturdayNightLights Jan 17 '15 at 21:25
  • You should just use `Parse.Object.saveAll` or every toSave object need 2 request (`Parse.cloud.run` and `object.save`) to save in previous approach. – eth3lbert Jan 18 '15 at 09:13

1 Answers1

0

The reason your code failed before you changed it to use saveAll() is that you started a bunch of async calls inside your for loop, and as soon as you finished starting the last one you then went on to call status.success() which terminates your code... you didn't wait for the async tasks to finish.

One way would be to create an array, put all your run() calls (which return promises) in there, then call success() inside the then block of Parse.Promise.when(promises).then(...).

var promises = [];
for (var i = 0; i < arrayLength; i++) {   
    console.log('saving Object ' + newTopics[i]);
    promises.push(Parse.Cloud.run('saveObject', {
        name: newObjects[i]
    }, {
        success: function (message) {
            console.log(message);
        },
        error: function (error) {
            console.log(error);
        }
    }));
}

Parse.Promises.when(promises).then(function() {
    status.success("New Objects Added.");
});

Promises are your friends, learn them, love them, and they will make your life easier!

Timothy Walters
  • 16,866
  • 2
  • 41
  • 49