解析Javascript API云代码afterSave并访问beforeSave值 [英] Parse Javascript API Cloud Code afterSave with access to beforeSave values

查看:88
本文介绍了解析Javascript API云代码afterSave并访问beforeSave值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我试图将值作为属性传递给responseresponse.object对象,但都没有传递给afterSave

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; 
});

有什么想法吗?

推荐答案

我对此进行了进一步研究,最终我将其保存为临时对象 并按如下所示将其删除:

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!

这篇关于解析Javascript API云代码afterSave并访问beforeSave值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆