我正在使用Parse Cloud Code进行保存之前/之后的处理。我会在afterSave挂钩中检查属性的beforeSave值-例如,当parseObj.get('status')processing => complete过渡时,请执行某些操作。我可以访问oldVal/newVal以更改beforeSave处理程序中的属性,但是我找不到在不另存为DB字段的情况下将oldVal传递给afterSave处理程序的方法。

我试图将值作为属性传递给responseresponse.object对象,但都无法传递给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;
});


有任何想法吗?

最佳答案

我仔细研究了一下,最终将其保存到Parse对象,并按如下所示将其删除:

//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()


希望对您有所帮助。祝好运!

09-19 10:28