Skip to content
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

How do I properly serialize Parse Objects in cloudcode BeforeSave? #3363

Closed
jeacott1 opened this issue Jan 12, 2017 · 3 comments
Closed

How do I properly serialize Parse Objects in cloudcode BeforeSave? #3363

jeacott1 opened this issue Jan 12, 2017 · 3 comments

Comments

@jeacott1
Copy link

jeacott1 commented Jan 12, 2017

if I have a Numeric field and increment it using the sdk something like

...
someTestClassQuery.find().then(function(results) {
 results[0].increment("MyNumericField");
 results[0].save();

and I have cloudcode something like:

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

var fieldValue=req.object.get('MyNumericField'); //this holds the expected field value, already incremented.

  var jsonBody = {};
  jsonBody.object = req.object.toJSON();
  jsonBody.object.className = req.object.className;

/*at this point,  jsonBody.object has lost its actual current value, and its replaced by an IncrementOp with a value of 1.

var unmarshalled = JSON.parse(jsonBody.object);
this object is not the same as  req.object. 
var fieldValueb=req.object.get('MyNumericField'); returns IncrementOp, not value
*/

how should I properly marshall/unmarshall parse objects such that they maintain their integrity?

thanks

@flovilmart
Copy link
Contributor

AFAIK That's unfortunately not possible with the current SDK's. The beforeSave has the client 'operations' and not the full values. an afterSave would have the proper values.

Also, possibly fixable with the internals of the JSSDK, if the operations are stored differently than the values.

@flovilmart
Copy link
Contributor

flovilmart commented Jan 12, 2017

See: https://github.com/ParsePlatform/Parse-SDK-JS/blob/master/src/ParseObject.js#L418

Any value set would be overridden by the op when calling the toJSON.
You can probably still access the underlying value (if available) through .get

@hramos
Copy link
Contributor

hramos commented Feb 10, 2017

Hey, thanks for reporting this issue.

Can you please add all the info specified in the template? This is necessary for people to be able to understand and reproduce the issue being reported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants