javascriptandroidparsingparse-platformparse-cloud-code

Can't figure out Parse Config with Cloud Code


Been searching for solutions for hours and getting close to no luck. I just don't see much documentation on the matter of Parse Config. At least with solutions because I tried everything I could find.

So basically I'm trying to set a default picture when someone saves an object with a status as "denied."

It started with me looking at this: Set default profile picture for parse signup

And here's what I got.

//Accepts/denies picture request
Parse.Cloud.afterSave("Requests", function(request) {
 
  var toUserId = request.object.get("to").id;
  var fromUserId = request.object.get("from").id;
  var toUser = null;
  var fromUser = null;
 
  var status = request.object.get("status");
 
    if (status === "accepted") {        
      .....
 
  } else if (status === "denied") {

    
    Parse.Config.get().then(function(config) { 

        request.object.set('photo', config.get("denied"));
    }).then(function() {
        console.log('Success: Denied photo set.');
    }, function(error) {
        console.log('error: denied photo not set');
    });
    

  } else if (status === "waiting") {
     ....
       
  }
});

I get a success everytime, but I get nothing as the photo file. I'm stuck and not sure what else to do here. The status changes to denied correctly, but I don't get anything to show up as a file in the photo spot, stays as undefined..

I2015-08-24T01:54:09.837Z]v46 after_save triggered for Requests for user oE3FhNfyWW:

  Input: {"object":{"createdAt":"2015-08-24T01:54:03.398Z","from":{"__type":"Pointer","className":"_User","objectId":"odv4R9OWso"},"objectId":"InB8Iods8U","status":"denied","to":{"__type":"Pointer","className":"_User","objectId":"oE3FhNfyWW"},"updatedAt":"2015-08-24T01:54:09.834Z"}}
  Result: Success
I2015-08-24T01:54:09.973Z]Success: Denied photo set.

Solution

  • I notice the code doesn't say request.object.save(), which might explain why the object isn't changed when you check later on.

    But saving seems strange, since this function runs after saving. That's either wasteful or infinitely-loopy. Since the goal is to modify request.object (the object just saved), then do this on beforeSave().

    Remember to call response.success() or .error() at the end of beforeSave().