0

I've got an iOS app I've written using Parse as the backend.

A user can store items in the system. An item has one or more pictures associated with it. By default, a user's items and pictures are accessible only by that user. A user can generate an alert for an item. When an item has an alert, public read access is given to the item and its pictures. When the alert expires, I want to revoke public read access for those objects.

Currently, I'm adding the public access through the iOS client when the alert is created. Because an alert can expire or be turned off by the user, I'm trying to handle the other half of the code in Parse's Cloud Code, in an after_save handler.

I'm currently trying the following:

if (alert.get("isActive") === false) {
    // This alert has been rendered inactive, so make sure all photos are not publically readable
    var photos = alert.get("photos");
    if (photos !== undefined && photos.length > 0) {
        for (var i = 0; i < photos.length; i = i + 1) {
            var photo = photos[i];
            photo.fetch().then(function () {                                
                var photoACL = photo.getACL();
                photoACL.setPublicReadAccess(false);
                photo.setACL(photoACL);
                photo.save();
            });                         
        }
    }
}

The alert has an Array named "photos". Each of those should be a pointer to an object with parseClassName "photo".

When I look at my console log, I see expected data for what is reported for the incoming alert. I see the alert and its properties, etc. Specifically, the "photos" array looks as I believe it should.

Problematically, though, I can't seem to figure out how to manipulate that array. The error I keep getting is that I can't call setPublicReadAccess on undefined. Great, so that means that photoACL is undefined, but WHY? The items in the alert.photos array are ParseObject instances. Shouldn't .fetch() work, then?

Also, when I try logging the objects themselves to the console (using console.log(JSON.stringify(photo))), I get oddly inconsistent results. Sometimes I just get {"objectId":"whatever"}, but sometimes I see the whole object with all of its properties included.

mbm29414
  • 11,558
  • 6
  • 56
  • 87

1 Answers1

1

Fetch passes the fetched object to the callback, it doesn't assign it to the original "photo" variable:

photo.fetch().then(function (photo) {
    // "photo" now refers to the fetched object
});

I think that should solve the undefined problem you encountered.

Tom Erik Støwer
  • 1,399
  • 9
  • 19
  • When I read your answer, I was sure you were wrong! I thought the item was implicitly populated with the results due to the "fetch" call, but I was wrong and you were right! Thanks! – mbm29414 Oct 26 '14 at 19:55