Skip to content

Parse.Object.save() does not return the same thing on parse-server and on Parse.com #653

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
Kira2 opened this issue Feb 25, 2016 · 6 comments

Comments

@Kira2
Copy link
Contributor

Kira2 commented Feb 25, 2016

Hi,

I have several Cloud Code functions that I call using the Rest API, and that update some objects. At the end of the functions, the modified object is returned by using response.success(). When I call the same functions with parse-server, the response is different.

This is an example to reproduce it.

Create a beforeSave trigger and a Cloud Code function like this:

/**
 * @description Sets the value of the field "auto" on each new book
 */
Parse.Cloud.beforeSave("Book", function(request, response) {

  if (request.object.isNew()) {
    var title = request.object.get("title");
    request.object.set("auto", title + "-auto");
  }

  response.success();
});

/**
 * @description A minimalist function that creates a new book and saves it
 */
Parse.Cloud.define("createBook", function(request, response) {
  var testObject = new Parse.Object("Book");
  testObject.set("title", "The Old Man and the Sea");

  testObject.save().then(function(result) {
    console.log(result); // we log the object returned by save()
    response.success(result);

  }).fail(function(err) {
    response.error(err);
  });
});

If the code is deployed on Parse.com, and you call the function "createBook" using the REST API, the new "Book" object is returned by response.success(). The line with console.log() displays the object returned by save():

{"title":"The Old Man and the Sea","auto":"The Old Man and the Sea-auto","objectId":"KTnRfSw3QW","createdAt":"2016-02-25T13:24:57.124Z","updatedAt":"2016-02-25T13:24:57.124Z"}

If you use parse-server locally, the result of the save() operation is different, and the object returned by response.success() is different too (the "auto" field is missing). The line with console.log() displays the object returned by save() on parse-server:

ParseObject { _objCount: 0, className: 'Book', id: 'XiDFZ3dczo' }

So, all my code that returns the result of the save() operation into response.success() does not work anymore.

Thanks.

@simonbengtsson
Copy link
Contributor

If I recall correctly this ParseObject { _objCount: 0, className: 'Book', id: 'XiDFZ3dczo' } is how all parse objects are shown in the logs even though they contain more data. What do you get if you do testObject.get('auto')?

@Kira2
Copy link
Contributor Author

Kira2 commented Feb 25, 2016

Hi,

Thank you for your response. Yes ParseObject { _objCount: 0, className: 'Book', id: '18Gut4HqO1' } is what is logged. And if I try to do testObject.get('auto') after the save() operation, I got undefined.

When I log the result of the REST API call, I also got the object without any of the modifications (in this example, the auto field is missing):

Object{result: Object{title: 'The Old Man and the Sea', createdAt: '2016-02-25T23:09:47.667Z', updatedAt: '2016-02-25T23:09:47.667Z', objectId: '18Gut4HqO1', __type: 'Object', className: 'Book'}}

Yet the modifications are done into the database.

I forgot to mention that I use the last release version of parse-server.
Thanks.

@gfosco
Copy link
Contributor

gfosco commented Feb 26, 2016

I don't think req.object.isNew() is valid, but req.object.existed() should work in version 2.1.3. When you want to log an object, try JSON.stringify(object) to get the standard format string instead of the ParseObject {} version.

@simonbengtsson
Copy link
Contributor

I actually managed to replicate this now and I'm in the process of tracking it down. I think this issue can be closed though as I'm pretty sure it is the same as #457 (as you mentioned) and it's better described there. @Kira2 Can you verify that it is the same problem by checking that the values gets correctly set in the database, just not passed to the beforeSave?

@Kira2
Copy link
Contributor Author

Kira2 commented Feb 26, 2016

Hi,

Sorry for the delayed response. @gfosco request.object.isNew() works fine for me since the last version. I have integration tests for my application that validate it. If you remove it from the beforeSave function, the problem is still the same.

@simonbengtsson The values are correctly set into the database. If I execute the code I wrote above, but by using JSON.stringify(object) like @gfosco suggested, this is what I have:

// logged into the function with console.log()
{
  "title":"The Old Man and the Sea",
  "createdAt":"2016-02-26T19:06:49.569Z",
  "updatedAt":"2016-02-26T19:06:49.569Z",
  "objectId":"gtOfbwTohf"
}
// when I log the result of the call of "createBook"
{
  "title":"The Old Man and the Sea",
  "createdAt":"2016-02-26T19:06:49.569Z",
  "updatedAt":"2016-02-26T19:06:49.569Z",
  "objectId":"gtOfbwTohf",
  "__type":"Object",
  "className":"Book"
}

Thanks.

@Kira2
Copy link
Contributor Author

Kira2 commented Mar 3, 2016

Hi,

As you said, I can close this one as it seems to be the same thing than #457 and that you gave a better description in the other post.

Thanks.

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