Skip to content

Bug about afterSave hook on increment operation #743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wangmengyan95 opened this issue Mar 1, 2016 · 4 comments
Closed

Bug about afterSave hook on increment operation #743

wangmengyan95 opened this issue Mar 1, 2016 · 4 comments
Labels
type:bug Impaired feature or lacking behavior that is likely assumed

Comments

@wangmengyan95
Copy link
Contributor

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

@wangmengyan95 wangmengyan95 added the type:bug Impaired feature or lacking behavior that is likely assumed label Mar 1, 2016
@nlutsenko
Copy link
Contributor

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

@wangmengyan95
Copy link
Contributor Author

@nlutsenko we need both.

@nlutsenko
Copy link
Contributor

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

@wangmengyan95
Copy link
Contributor Author

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
Labels
type:bug Impaired feature or lacking behavior that is likely assumed
Projects
None yet
Development

No branches or pull requests

2 participants