user3786591
user3786591

Reputation: 73

Update records in Parse with Geopoints

I have about 600,000 records I uploaded through the data uploader in CSV format. My longitude and latitude columns are separate. I'm trying to modify the class in cloud code with this script. It updates sometimes and then other times there is an error. Can someone help me with this script or is there a way to do this that I'm not aware of.

Parse.Cloud.job("CreatePoints", function(request, status) {

    // Set up to modify user data
    Parse.Cloud.useMasterKey();

    var recordsUpdated = 0;

    // Query for all objects with GeoPoint location null
    var query = new Parse.Query("Class");
    query.doesNotExist("location");
    query.each(function(object) {
        var location = {
            latitude: object.get("latitude"),
            longitude: object.get("longitude")
        };
        if (!location.latitude || !location.longitude) {
            return Parse.Promise.error("There was an error.");
        }

        recordsUpdated += 1;
        if (recordsUpdated % 100 === 0) {
            // Set the job's progress status
            status.message(recordsUpdated + " records updated.");
        }

        // Update to GeoPoint
        object.set("location", new Parse.GeoPoint(location));
        return object.save();
    }).then(function() {
        // Set the job's success status
        status.success("Migration completed successfully.");
    }, function(error) {
        // Set the job's error status
        console.log(error);
        status.error("Uh oh, something went wrong!");
    })
});

Upvotes: 4

Views: 910

Answers (1)

Timothy Walters
Timothy Walters

Reputation: 16884

As per the comments, your issue is that some of the Class members have no longitude or latitude.

You could change your query to only process those that have both values:

var query = new Parse.Query("Class");
query.doesNotExist("location");
query.exists("longitude");
query.exists("latitude");
query.each(function(object) {
    // etc

Then you no longer need to check for them being empty, no longer need to return a Parse.Promise.error(), so should no longer hit your error.

Upvotes: 1

Related Questions