解析API的Javascript代码的云与afterSave访问beforeSave值(Parse

2019-10-23 02:38发布

我使用的解析云代码前/ afterSave处理的事情。 我检查在afterSave钩属性的beforeSave值-例如,做一些事情时parseObj.get('status')从转换processing => complete 。 我可以访问oldVal/newVal用于改变属性的beforeSave处理程序,但我不能找到一种方法来传了oldValafterSave处理程序,不保存为DB场。

我试图通过的值作为对一个属性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; 
});

有任何想法吗?

Answer 1:

我看着这一些,我结束了暂时保存到解析对象,这样的删除它:

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

我希望帮助。 祝好运!



文章来源: Parse Javascript API Cloud Code afterSave with access to beforeSave values