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

Responding to a cloud function with a parse object #985

Closed
cyboolo opened this issue Mar 11, 2016 · 11 comments
Closed

Responding to a cloud function with a parse object #985

cyboolo opened this issue Mar 11, 2016 · 11 comments
Labels
type:feature New feature or improvement of existing feature

Comments

@cyboolo
Copy link

cyboolo commented Mar 11, 2016

Hello

I have a simple function to get User object:

Parse.Cloud.define("getMB", function(request, response) {
    var myId = request.params.A;
    Parse.Cloud.useMasterKey();

    var query = new Parse.Query(Parse.User);

    query.get(myId, {
    success: function(object) {

    response.success(object);    

    },
    error: function(object, error) {

    response.error(error);    

    }
    });

  });

Before I was with parse server version 2.0.0 and now I have updated to last version.

On 2.0.0 I could get all the fields for my User (more than 20).

And now, I just get this response on the client:
t {_objCount: 0, className: "_User", id: "LPoRpS4Z6P"}

I did not change my cloud code. Login is working but I have bugs after login when I want to get User object.

Could you tell me how to deal with this issue?

Environment Setup

Mac / Chrome

@cyboolo
Copy link
Author

cyboolo commented Mar 12, 2016

It seems that I need to JSON.stringify my object response.

Why do we have this change?

@gfosco gfosco changed the title Problem with parse server updating Responding to a cloud function with a parse object Mar 12, 2016
@flovilmart
Copy link
Contributor

@cyboolo yes you have to stringily the JSON object as the result is passed directly to the network response. You can call response.success(object.toJSON()).

@flovilmart
Copy link
Contributor

It seems that as of #1066 I properly receive a Parse.Object in the client

@drew-gross
Copy link
Contributor

Seems to be fixed and no response in many days, closing.

@DFranch
Copy link

DFranch commented Apr 26, 2016

Hi guys,

We are in the process of migrating from Parse.com to our own hosted server using parse-server and had this issue.

We experienced that using response.success(object) (without .toJSON()) worked if we returned the Parse.Object without any changes from a Parse.Query. It would return the entire Parse.Object.

However, as soon as we added attributes to the object with .set() :

thisIsAnParseObject.set("foo", "bar")
response.success(thisIsAnParseObject)

Only the following information from the cloud code was returned to the client:

{"result":[
{"__type":"Pointer","className":"Moment","objectId":"Gw3jXrg1UM"}
]}

When using response.success(thisIsAnParseObject.toJSON() the entire object was returned to the client.

Is it documented anywhere that .toJSON() is needed in order to return the intended data? Just trying to save others the time we have spent of debugging this issue 👍

parse-server version: 2.2.7
parse-js-sdk: 1.8.3

@parkej60
Copy link

This seems to work correctly if you call .toJSON on the top level object, but any of it's properties that contain objects are not affected, they still just pass down the reference. How would you correctly go about handling the scenario where you have an object that has a relation to a user reference.

@danielchangsoojones
Copy link

danielchangsoojones commented Jun 5, 2017

When I was using Parse promises from an outside node.js file to my main.js Cloud file. I was running into a similar problem where Parse Objects were returning without a __type field like:

ParseObject { _objCount: 4, className: 'Order', id: '0isM878h7U' }

So, when I would received the result in my iOS application via a Cloud Code call, it wasn't understanding that my Parse Object was supposed to be an Order. It just saw it as JSON. So, I had to use:

Parse._encode(order)

I used this encoding function before I resolved my order object to the cloud code main.js file, and my Order Object printed out as:

{ note: 'testing for creating pick list', name: '#HIPPIESANDHOUSEWIVEWundefined', shipmentStatus: 'open', createdAt: '2017-06-05T03:54:44.912Z', updatedAt: '2017-06-05T03:54:44.912Z', objectId: '0isM878h7U', __type: 'Object', className: 'Order' }

You can see the __type field was retained, so now my iOS application receives a properly typed object. This took me a while to figure out, and toJSON/JSON.stringify were not working for me, but ultimately encoding did the correct thing. I hope this might help someone else.

@mpcmarques
Copy link

Using version 2.6.5 here.
Bug still persist, when querying with include I was observing that the nested objects from the object returned from the cloud code were with undefined attributes.
I modified the return code to result.toJSON() and got the right object, so for now the bug is still happening.

Example:
Cloud code:

Parse.Cloud.define(GET_PRODUCT, function(request, response){
  var productId = request.params.id;
  
  var Product = Parse.Object.extend("Product");
  var query = new Parse.Query(Product);
  
  // include nutritional table
  query.include("nutritionalTable");
  query.include("ingredients");

  query.get(productId).then(function(result){

    // fetch ingredients
    Parse.Object.fetchAll(
      result.get("ingredients")
    ).then(function(ingredients){

      // update ingredients list
      result.set("ingredients", ingredients);

      console.log(result.get("nutritionalTable").get("cells")); // returns the correct attribute.

      // success
      response.success(result.toJSON());
    });
  });
});

Javascript sdk code:

Parse.Cloud.run(GET_PRODUCT, { id: productId }).then(function( result ) {
   console.log(result.get("nutritionalTable").get("cells")); // returns undefined    
 });

@trinadhkoya
Copy link

@cyboolo yes you have to stringily the JSON object as the result is passed directly to the network response. You can call response.success(object.toJSON()).

But where as in the failure response , if i tried to do the same it thrown an error and by default it is responding a valid JSON response

@davimacedo
Copy link
Member

This is a pretty old thread regarding old versions of Parse. If you use the latest version, the code below should work:

Parse.Cloud.define('getMB', async request => {
  const myId = request.params.A;
  const query = new Parse.Query(Parse.User);
  return await query.get(myId, { useMasterKey: true });
});

@wqzyow
Copy link

wqzyow commented Jan 5, 2020

Exact same issue as @DFranch. As soon as you use .set() on the object, the server will return undefined values for attributes (or no attributes at all).

Parse.Cloud.define('getClassA', async request => {
  const objectId = request.params.id;
  const query = new Parse.Query("ClassA");
  const object = await query.get(objectId, { useMasterKey: true });
  object.set("newAttribute", "someValue");
  return object; 
});

If you save the object (object.save()) before returning it to client however, attributes will have values including newAttribute.

object.set("newAttribute", "someValue");
return await object.save(null, { useMasterKey: true });

Returning JSON.stringify(object) to client without saving object first also returns object in JSON format containing all attributes and their values.

object.set("newAttribute", "someValue");
return JSON.stringify(object); 

server version: 3.9.0 (but issue happens with earlier versions as well)
client sdk: 2.10.0 (but issue happens with earlier versions as well).

@mtrezza mtrezza added type:feature New feature or improvement of existing feature and removed type:improvement labels Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

No branches or pull requests