Maxime L
Maxime L

Reputation: 178

Save "object without data" pointer in Parse Cloud Code

Is it possible to save "empty" objects in an object to save the reference without having to fetch the full objects before ?

I'm doing this :

var myObject = new MyObject();
myObject.id = "id I retrieved somewhere on my client device"

// later
var user = request.user;
user.set("key", myObject);

But it's not working, it's saying "Cannot create a pointer to an unsaved ParseObject"

Upvotes: 0

Views: 2439

Answers (2)

Pung Worathiti Manosroi
Pung Worathiti Manosroi

Reputation: 1490

I think createWithoutData method is what you're looking for. So in your case it'll be something like:

 var ClassOfMyObject = Parse.Object.extend("ClassOfMyObject");
 var myObject = ClassOfMyObject.createWithoutData("myObjectId");

 // later
 var user = request.user;
 user.set("key", myObject); 

Hope this answer your question.

Upvotes: 5

Chris L
Chris L

Reputation: 1061

Solution 1: (not really a solution, just explanation)

To create a pointer or relation in Parse.com you have to first have an object to place there.

Solution 2: However, you could create a text column in your Class and reference by these text properties instead of using the Parse relations. Essentially, create your own Id for that object stored as a text value.

If you decide to go this route you will need to make sure you have essentially a unique Id since it will be difficult to auto increment with Parse. I use this solution myself.

Create GUID / UUID in JavaScript?

function guid() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
  .toString(16)
  .substring(1);
}
 return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
 s4() + '-' + s4() + s4() + s4();
}

var uuid = guid();

Which I place in an angular factory to use in my SPAs.

The only problem with using a text field and referencing by those values is that it counts towards queries and the various limits on them. If you have a relation you can return the relations without repeating queries.

I typically use a combination of the two depending on the needs of each particular data set.

Solution 3:

With regards to having an "empty object." Yes, you could create an empty object and THEN populate it with data. So long as the object is created in Parse and has an objectId it can be used to create a relation.

But be sure that all objects in the relation pattern have been created and saved prior to creating the relation and then saved again.

Upvotes: 0

Related Questions