Skip to content

Bug about afterSave hook on increment operation #743

Closed

Description

@wangmengyan95

Make sure these boxes are checked before submitting your issue -- thanks for reporting issues back to Parse Server!

-[ ] You've met the prerequisites.

-[ ] You're running the latest version of Parse Server.

-[ ] You've searched through existing issues. Chances are that your issue has been reported or resolved before.

Environment Setup

Steps to reproduce

Step 1. Create a new ParseObject with a number field like this

 curl -X POST -H "X-Parse-Application-Id: myAppId"   -H "Content-Type: application/json"   -d '{"score":1}'   http://localhost:1337/parse/classes/Game

Step 2. Add a afterSave hook for this class like

Parse.Cloud.afterSave('Game', function(req) {
  console.log(req.object);
  console.log(req.object.get('score'));
});

Step 3. Update the object created in step 1 with an increment operation like

curl -X PUT   -H "X-Parse-Application-Id: myAppId"   -H "Content-Type: application/json"   -d '{"score":{"__op":"Increment","amount":100}}'   http://localhost:1337/parse/classes/Game/XXXX.

Step 4. Check the log of the afterSave hook
The score field is still an operation, not the actual value.

I verify the behaviour of Parse.com, the value we get in the afterSave is a number not the operation.

Logs/Trace

Activity

  1. added
    type:bugImpaired feature or lacking behavior that is likely assumed
    on Mar 1, 2016
  2. nlutsenko commented on Mar 2, 2016

    @nlutsenko
    Contributor

    @wangmengyan95 Since you are on it, do we expect to have originalObject in the afterSave?
    Or only the object?

  3. wangmengyan95 commented on Mar 2, 2016

    @wangmengyan95
    ContributorAuthor

    @nlutsenko we need both.

  4. nlutsenko commented on Mar 2, 2016

    @nlutsenko
    Contributor

    And few more questions - inside afterSave - does object have all the changes applied?
    Or all the changes are marking the object as dirty?

  5. wangmengyan95 commented on Mar 2, 2016

    @wangmengyan95
    ContributorAuthor

    It seems we do not support dirtyKeys in afterSave in classic Parse.com.
    This is the cloud function I use

    Parse.Cloud.beforeSave('Game', function(req, res) {
      console.log('beforeSave');
      var dirtyKeys = req.object.dirtyKeys();
      console.log('dirtyKeys size : ' + dirtyKeys.length);
      for (var i = 0; i < dirtyKeys.length; ++i) {
        var dirtyKey = dirtyKeys[i];
        console.log(dirtyKey);
      }
      req.object.set('beforeSave', 1);
      res.success();
    });
    
    
    Parse.Cloud.afterSave('Game', function(req) {
      console.log('afterSave');
      var dirtyKeys = req.object.dirtyKeys();
      console.log('dirtyKeys size : ' + dirtyKeys.length);
      for (var i = 0; i < dirtyKeys.length; ++i) {
        var dirtyKey = dirtyKeys[i];
        console.log(dirtyKey);
      }
    });
    

    If I create a Game object and set the score field, the log is

    I2016-03-02T20:31:11.627Z]v5 before_save triggered for Game as master:
      Input: {"original":null,"update":{"score":888}}
      Result: Update changed to {"beforeSave":1,"score":888}
    I2016-03-02T20:31:11.643Z]beforeSave
    I2016-03-02T20:31:11.644Z]dirtyKeys size : 1
    I2016-03-02T20:31:11.645Z]score
    I2016-03-02T20:31:11.649Z]v5 after_save triggered for Game as master:
      Input: {"object":{"beforeSave":1,"createdAt":"2016-03-02T20:31:11.646Z","objectId":"WgXN1eTPqW","score":888,"updatedAt":"2016-03-02T20:31:11.646Z"}}
      Result: Success
    I2016-03-02T20:31:11.678Z]afterSave
    I2016-03-02T20:31:11.679Z]dirtyKeys size : 0
    

    If I update a Game object with a new score, the log is

    I2016-03-02T20:30:50.360Z]v5 before_save triggered for Game as master:
      Input: {"original":{"beforeSave":1,"createdAt":"2016-03-02T20:28:56.642Z","objectId":"5GTUPMzSzV","score":345,"updatedAt":"2016-03-02T20:28:56.642Z"},"update":{"score":666}}
      Result: Update changed to {"beforeSave":1,"score":666}
    I2016-03-02T20:30:50.374Z]beforeSave
    I2016-03-02T20:30:50.375Z]dirtyKeys size : 1
    I2016-03-02T20:30:50.376Z]score
    I2016-03-02T20:30:50.380Z]v5 after_save triggered for Game as master:
      Input: {"object":{"beforeSave":1,"createdAt":"2016-03-02T20:28:56.642Z","objectId":"5GTUPMzSzV","score":666,"updatedAt":"2016-03-02T20:30:50.378Z"}}
      Result: Success
    I2016-03-02T20:30:50.393Z]afterSave
    I2016-03-02T20:30:50.394Z]dirtyKeys size : 0
    

    In any cases, the dirtyKeys size in afterSave is always 0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugImpaired feature or lacking behavior that is likely assumed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions