0

I have a parent object "Listing" (think real estate listing), that can have multiple child objects "Image".

I'm trying to implement a Cloud Code function that marks all the child objects as archived when I archive their parent.

For some reason the query result is always empty. I can't see why. My "Error: image undefined" error appears each time.

The Image class has a pointer to Listing, but there's no relation from Listing to Image.

Parse.Cloud.afterSave("Listing", function(request) {

    Parse.Cloud.useMasterKey();

    // Handle archiving. If a listing is marked as archived, mark the image as archived also.

    query = new Parse.Query("Image");
    query.equalTo("listing", request.object);

    query.first({
        success: function(image) {

            if (typeof image != "undefined") {
                image.archived(request.object.get("archived"));
                image.save();

                console.log("Done");
            }
            else {
                console.log("Error: image undefined.");
            }
        },
        error: function(error) {
            console.error("Got an error " + error.code + " : " + error.message);
        },
    });
);

Any help appreciated.

That Guy
  • 460
  • 1
  • 7
  • 17

1 Answers1

0

I've been able to get this to work using the following:

Parse.Cloud.afterSave("Listing", function(request) {

    Parse.Cloud.useMasterKey();

    // Handle archiving. If a listing is marked as archived, mark the image as archived also.

    query = new Parse.Query("Image");
    query.equalTo("listing", request.object);

    query.find({
        success: function(images) {

            if (typeof images != "undefined") {

                for (i = 0 ; i < images.length; i++) {

                    var theImage = images[i];
                    theImage.set("archived", request.object.get("archived"));
                    theImage.save();
                }

                console.log("Done");
            }
            else {
                console.log("Error: image undefined.");
            }
        },
        error: function(error) {
            console.error("Got an error " + error.code + " : " + error.message);
        },
    });
);
That Guy
  • 460
  • 1
  • 7
  • 17