1

I'm using Parse Cloud Code to do before/afterSave processing. I'd check the beforeSave value of an attribute in the afterSave hook - for example, do something when parseObj.get('status') transitions from processing => complete. I can get access to the oldVal/newVal for changed attributes in the beforeSave handler, but I can't find a way to pass the oldVal to the afterSave handler without saving as a DB field.

I tried to pass the values as an attribute to the response and response.object objects, neither makes it through to the afterSave

Parse.Cloud.beforeSave('ParseObj', function(request, response) {
  var checkDirty, isDirty, parseObj, prevQ;
  // request keys=["object","installationId","user","master"]
  if (request.object.isNew()) {
    return response.success();
  } else {
    parseObj = request.object;
    checkDirty = ['status']; // array of all attrs to check
    isDirty = [];
    _.each(checkDirty, function(attr) {
      if (parseObj.dirty(attr)) {
        isDirty.push(attr);
      }
    });
    console.log("isDirty=" + JSON.stringify(isDirty));
    if (isDirty.length) {
      prevQ = new Parse.Query('ParseObj');
      return prevQ.get(parseObj.id).then(function(prevParseObj) {
        var newVal, oldVal;
        console.log("prevParseObj, id=" + prevParseObj.id);
        oldVal = _.pick(prevParseObj.toJSON(), isDirty);
        _beforeSave(parseObj, oldVal);  // do something here
        request.previousValues = oldVal
        request.object.previousValues = oldVal
        return response.success();
      }, function(err) {
        return response.error("parsObj NOT FOUND, parseObj.id=" + parseObj.id);
      });
    } else {
      return response.success();
    }
  }
});

Parse.Cloud.afterSave('ParseObj', function(request) {
  var parseObj;
  // request keys=["object","installationId","user","master"], 
  oldVal = request.previousValues  // undefined
  oldVal = request.object.previousValues // also, undefined
  parseObj = request.object;
  _afterSave(parseObj, oldVal)  // do something else here
  return; 
});

Any ideas?

michael
  • 4,377
  • 8
  • 47
  • 73
  • Did you ever figure this out? I'm trying to do something similar. I need to know the old value on a record during `afterSave` in addition to the new value. – Clifton Labrum May 23 '15 at 00:13
  • unfortunately, no. I can't figure out how to pass as value between the beforeSave and afterSave hooks unless I save to the Parse.Object - but that's sort of dumb. – michael May 25 '15 at 08:37

1 Answers1

0

I looked into this some more, and I ended up saving to the Parse object temporarily and removing it like this:

//Add the value you need to the object prior to saving using an "oldValue" key
yourPFObject["oldValue"] = value (this is in your SDK somewhere where the save happens)

//Get that value in afterSave in Cloud Code
var old = object.get("oldValue")

//Do whatever you need to do with "old"...

//Remove the temporary old value so it doesn't stay in the database
object.unset("oldValue") 
object.save()

I hope that helps. Good luck!

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128