0

I am trying to retrieve/fetch a current object from the beforeSave method as I want to know the value of "Parent" column before update was initiated.

I used the below code. However, I see that older values and newer values remain equal though value of "Parent" has been changed through data browser.

Is there any suggestion to read the current object from beforeSave? Is anything going wrong below?

Parse.Cloud.beforeSave("Products", function(request, response) {
    var oldObject = new Parse.Object('Products');
    oldObject.id = "RTRzAf1FyX"; // RTRzAf1FyX is equal to request.object.id. oldObject.id is being hard coded to RTRzAf1FyX

    listOfAllPromisesForCurrentObject.push(oldObject.fetch());

    Parse.Promise.when(listOfAllPromisesForCurrentObject).then(function(currentObject) {

      console.log("11111 +++++++++++++++" + JSON.stringify( request.object.get('Parent')) ); //Parent is a column name
      console.log("11111 ---------------" + JSON.stringify( currentObject.get('Parent')) );

});
egor.zhdan
  • 4,555
  • 6
  • 39
  • 53
  • See http://stackoverflow.com/questions/31100446/accessing-original-field-in-parse-com-cloud-code-beforesave-function – danh Nov 12 '15 at 20:28

1 Answers1

0

You can't get those old values. Only whether or not the key is dirty. The only suggestion I have is to have two pointers, parent and oldParent, and when you're updating parent, do oldParent = parent first.

Jake T.
  • 4,308
  • 2
  • 20
  • 48
  • Untrue, I think. See http://stackoverflow.com/questions/31100446/accessing-original-field-in-parse-com-cloud-code-beforesave-function – danh Nov 12 '15 at 20:30
  • @danh No , it did not work. In fact, Your solution depends on "query.get(object.id)." returning a old value. However , that's not happening in the latest parse release. Not Sure about Parse's earlier behaviour. – Parse User Nov 13 '15 at 03:40
  • It appears that @jake-t suggestion is the only way out , for this problem. Would be glad if some other solution exists. – Parse User Nov 13 '15 at 03:45
  • I'd recommend removing the oldParent pointer in the beforeSave trigger after you're done using it, to avoid fetching/saving an extra object when you fetch/save the Products object. – Jake T. Nov 13 '15 at 20:20