Or Rapaport
Or Rapaport

Reputation: 71

Parse.com cloud function - manually modify object fields before sending to client

I'm trying to limit the visibility of some fields of parse User object in cloud function. I have a "Product" class, with a pointer named "owner" to a the "User" that uploaded the item.

I also have a cloud function called "getProducts", I use query.include("owner") to get the owner data at the same time.

What i want to achieve, is that the output of the "getProduct", will be a list of products, but the "Owner" object will contain only certain fields, such as "firstName" or "facebookId", I don't want to return to the client other sensitive data even though I'm not presenting it (such as Location, email, family name etc..).

After searching I've seen 2 possible solutions. 1.) Cut the User class into 2 classes, 1 of is "Private" class with ACL just for the user. 2.) The second approach that i prefer, i to edit the fields in the cloud function, but i can't seem to change the "owner" object at the "product" object. i'm getting the error: "Error: Uncaught Tried to save an object with a pointer to a new, unsaved object. (Code: 141, Version: 1.2.19)"

var output[] = [];
_.each(results, function(result) {
    var responseData = {};
    var owner = result.get("owner");

    //Remove fields from the user object
    var itemOwnerId = owner.id;
    var itemOwnerFirstName = owner.firstName;
    var itemOwnerFacebookID = owner.facebookID;
    var itemOwner = new Parse.User();
    itemOwner.id = itemOwnerId;
    itemOwner.id = itemOwnerId;
    itemOwner.firstName = itemOwnerFirstName;
    itemOwner.facebookID = itemOwnerFacebookID;
    result.set("owner", itemOwner);
    responseData.item = result;
    output.push(responseData);
});

It seems that calling result.set("owner", itemOwner) isn't good, and throwing me exepction: rror: Uncaught Tried to save an object with a pointer to a new, unsaved object. (Code: 141, Version: 1.2.19)

What am I doing wrong?

Upvotes: 3

Views: 765

Answers (1)

Fosco
Fosco

Reputation: 38526

The SDK doesn't allow an object that has been changed to be serialized into a response.

A hack way to work around this would be:

result.dirty = function() { return false; };

This would disable the check and allow you to return the modified object.

If you wanted to re-enable it later, you'd need to store the original value of result.dirty and reassign it later.

Upvotes: 13

Related Questions