简体   繁体   English

在“解析云代码”中保存“没有数据的对象”指针

[英]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" 但是它不起作用,它说“无法创建指向未保存的ParseObject的指针”

I think createWithoutData method is what you're looking for. 我认为createWithoutData方法是您想要的。 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. 希望这个回答您的问题。

Solution 1: (not really a solution, just explanation) 解决方案1:(不是真正的解决方案,只是说明)

To create a pointer or relation in Parse.com you have to first have an object to place there. 要在Parse.com中创建指针或关系,您首先必须在该对象中放置一个对象。

Solution 2: However, you could create a text column in your Class and reference by these text properties instead of using the Parse relations. 解决方案2:但是,您可以在Class中创建一个文本列,并通过这些文本属性进行引用,而不是使用Parse关系。 Essentially, create your own Id for that object stored as a text value. 本质上,为存储为文本值的对象创建自己的ID。

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. 如果您决定采用这种方式,则需要确保您具有一个唯一的ID,因为使用Parse很难自动递增。 I use this solution myself. 我自己使用此解决方案。

Create GUID / UUID in JavaScript? 在JavaScript中创建GUID / UUID?

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. 我将其放置在有角度的工厂中以在SPA中使用。

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: 解决方案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. 只要对象是在Parse中创建的并且具有objectId,它就可以用于创建关系。

But be sure that all objects in the relation pattern have been created and saved prior to creating the relation and then saved again. 但是,请确保在创建关系之前已创建并保存了关系模式中的所有对象,然后再次保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM