Skip to content

In beforeSave, when creating a new object, dirty(:key) returns a different value than expected from dirtyKeys #1882

Closed
@natanrolnik

Description

@natanrolnik

Environment Setup

  • Server: 2.2.10 running on Dokku on AWS
  • Database: Remote, Mongo 3.0, also on AWS

Steps to reproduce

  • Setup a beforeSave hook for some class, let's say Event;
  • Create/Save an object, without setting the properties foo and otherKey with no changes, but setting a third one, thirdKey for example, to some value.
  • Add the following code sample:

Code sample

Parse.Cloud.beforeSave("Event", function(request, response)  {

  var event = request.object;
  console.log(event.dirtyKeys());

  if(!request.master) {
    var protectedKeys = ["foo", "otherKey"]; //only masterKey can change the protectedKeys
    for (i = 0; i < protectedKeys.length; i++) {
      var key = protectedKeys[i];
      if (event.dirty(key)) {
        response.error("External users are not authorized to change the key '" + key + "'. It's value is: (" + event.get(key) + "). Dirty keys are: " + event.dirtyKeys());
        return;
      }
    }
  }
});

Result

This same code works totally fine in the hosted Parse.com Cloud Code.
Unfortunately, event.dirty('foo') returns true, while dirtyKeys() does not contain the property 'foo'. I show an alert in my app when this error happens, and we can see the error:
captura de tela 2016-05-23 as 22 33 14

Mentioning @nlutsenko as he worked on dirtyKeys and beforeSave on #614 and @drew-gross on beforeSave on #1884

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions