我正在使用Parse.com javascript SDK,并且我试图找出如何使用指向另一个尚不存在的对象的指针来创建对象。例如,假设我有三个类Car,Wheel,Bumper。汽车对象包含指向车轮和保险杠的指针。我循环一个数组来创建汽车,我想创建一个保险杠对象和一个车轮对象,但我对这样做的最佳方法感到困惑。
var Car = Parse.Object.extend("Car");
var car = new Car();
car.set("name", "the name");
car.set("color", "the color");
car.save(null, {
success: function(album) {
// Execute any logic that should take place after the object is saved.
alert('New object created with objectId: ' + car.id);
},
error: function(car, error) {
// Execute any logic that should take place if the save fails.
// error is a Parse.Error with an error code and message.
alert('Failed to create new object, with error code: ' + error.message);
}
});
我先创建汽车对象吗?然后创建其他每个wto对象并稍后将它们保存为指针?
答案 0 :(得分:0)
必须先存在另外两个对象(保存到Parse),然后才能在Car对象中添加指针。
如果Wheel and Bumper对象尚不存在,您可以使用以下内容:
// Create new Wheel -> assign attributes -> save object
// Create new Bumper -> assign attributes -> save object
car.add("wheel", wheelObject);
car.add("bumper", bumperObject);
car.save(null, { ...
As mentioned here如果您尝试使用指向未保存对象的指针执行保存,则会出现以下错误
{"code":141,"error":"Uncaught Tried to save an object with a pointer to a new, unsaved object."}