parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

BeforeSave changes are not propagated into the result of a PUT operation with REST API

Open Kira2 opened this issue 10 years ago • 6 comments

Hi,

It seems that the changes into the beforeSave trigger are not propagated to the result of a PUT operation, even if they are correctly saved into the database. I'm not sure if this is the same problem as reported into #1031, because I have it only when I update an object, not when I create it.

Environment Setup

"parse": "^1.7.1", "parse-server": "^2.1.6",

Steps to reproduce

Suppose you have a class named Book. Create a beforeSave trigger that increments a property to 10, each time a book is saved:

// main.js
Parse.Cloud.beforeSave("Book", function(request, response) {
  var test = request.object.get("test");
  request.object.set("test", test + 10);
  response.success();
});

Create a Book like this, on Parse.com and on parse-server:

curl -X POST \
  -H "X-Parse-Application-Id: appId" \
  -H "X-Parse-REST-API-Key: restApiKey" \
  -H "Content-Type: application/json" \
  -d '{"title":"The old man and the sea","test":0}' \
  http://localhost:1337/parse/classes/Book

Now, update your Book like this, on Parse.com and on parse-server:

curl -X PUT \
  -H "X-Parse-Application-Id: appId" \
  -H "X-Parse-REST-API-Key: restApiKey" \
  -H "Content-Type: application/json" \
  -d '{"title":"Don Quichotte"}' \
  http://localhost:1337/parse/classes/Book/the_object_id

Logs/Trace

The result of POST on Parse.com:

{
  "objectId":"gwMrwEKQ1K",
  "createdAt":"2016-03-15T12:14:51.583Z",
  "title":"The old man and the sea",
  "test":10
}

The result of POST on parse-server:

// OK
{
  "objectId":"uH7K8zMfxD",
  "createdAt":"2016-03-15T12:04:27.931Z",
  "updatedAt":"2016-03-15T12:04:27.931Z",
  "title":"The old man and the sea",
  "test":10
}

The result of PUT on Parse.com:

{
  "updatedAt":"2016-03-15T12:15:13.811Z",
  "title":"Don Quichotte",
  "test":20
}

The result of PUT on parse-server:

// THE UPDATED FIELD ARE MISSING
{
  "updatedAt":"2016-03-15T12:06:13.163Z"
}

In both cases, the properties are correct into the database, but the response of the PUT operation is incomplete with parse-server, whereas it contains all the updated fields with Parse.com.

Kira2 avatar Mar 15 '16 12:03 Kira2

Thanks for reporting! I'll check it out!

flovilmart avatar Mar 15 '16 12:03 flovilmart

Just FYI, given the REST API documentation, the response object is not supposed to hold the updated keys. That would be an enhancement then.

@gfosco @drew-gross, was the returned key undocumented?

flovilmart avatar Mar 16 '16 12:03 flovilmart

Hi,

Thanks for your responses.

My current application uses it to synchronize the object on the client-side with what it is into the database after a beforeSave operation that updates the fields. So, the things will not work properly on parse-server because of that. Why there is this difference on parse-server, or between POST and PUT operations?

What should I do? Re-fetch each object after each save operation to be sure that the fields are correctly synchronized with what they are into the database after a beforeSave operation? Or remove the beforeSave triggers and rewrite each client to handle it on their own side?

Thanks.

Kira2 avatar Mar 16 '16 13:03 Kira2

POST is to create an object, PUT to update one.

You can re-fetch the objects, but I understand that can be problematic. What client do you use in your application? Do you only do REST calls?

flovilmart avatar Mar 16 '16 13:03 flovilmart

Yes, I use pure REST API calls.

I know that POST is to create and PUT to update. I just don't know why PUT could not return the fields with the updated values like POST does.

Kira2 avatar Mar 16 '16 13:03 Kira2

it's just not implemented that way, PR are welcome.

flovilmart avatar Mar 16 '16 13:03 flovilmart