parse-community / parse-server

Parse Server for Node.js / Express
https://parseplatform.org
Apache License 2.0
20.84k stars 4.77k forks source link

In afterSave, when creating a new object, dirty(:key) returns always 'false' #6874

Closed Cloudtrackers closed 4 years ago

Cloudtrackers commented 4 years ago

New Issue Checklist

Issue Description

Server: 4.3.0 Database: Mongo 4.0,

Steps to reproduce

Parse.Cloud.afterSave("Profile", async (req) => { let token = { sessionToken: req.user.getSessionToken() }; console.log("-> ", req.original, req.object.dirty("isActive")); });

Actual Outcome

req.object.dirty("keyname"); always return false than actual result.

Expected Outcome

if the particular key attribute value is changed, 'req.object.dirty("keyname");' should be true else false.

Environment

Server: 4.3.0 Database: Mongo 4.0,

Server

Database

Client

Logs

dplewis commented 4 years ago

I believe this is proper functionality. Saving an object clears dirty keys. If you want to check dirty keys try beforeSave.

Cloudtrackers commented 4 years ago

I believe this is proper functionality. Saving an object clears dirty keys. If you want to check dirty keys try beforeSave.

it's not working in afterSave hook. in afterSave, In the case of Updating the value, dirty keys always returns empty array.

dplewis commented 4 years ago

Can you write a failing test here. This will help us replicate your issue.

Cloudtrackers commented 4 years ago

Can you write a failing test here. This will help us replicate your issue.

Parse.Cloud.afterSave("Profile", async (req) => { let token = { sessionToken: req.user.getSessionToken() }; console.log(req.object.dirtyKeys()); });

just try this code in an sample code. dirtyKeys always returns empty array.

mtrezza commented 4 years ago

As @dplewis already pointed out, after saving an object, there are no dirty keys. A key is only dirty after its value has been modified and before its new value has been saved to the database.

  1. object.set(key, "newValue"); --> object is dirty
  2. object.save(); --> request to save new value to database
  3. beforeSave trigger is called --> object is dirty
  4. new value is saved to database
  5. afterSave trigger is called --> object is not dirty
mtrezza commented 4 years ago

I'm closing this as it does not seem to be a Parse Server issue.

For help with Parse Server, here are some resources you can try:

Feel free to comment if you have any questions and we can re-open this issue.