简体   繁体   English

解析云-删除属性(指针)

[英]Parse Cloud - delete property (pointer)

My data model consist of 3 objects, two of them (the children) are linked to the parent using a pointer. 我的数据模型由3个对象组成,其中两个(子代)使用指针链接到父代。

MyModel is the parent that has 2 properties: colors and goal. MyModel是具有2个属性的父对象:颜色和目标。 Both are pointers to other Objects. 两者都是指向其他对象的指针。

When i delete the parent I want the children to be deleted as well, the caveat is that the pointer might be nil, so I'd need to check if there is something there before attempting deletion. 当我删除父母时,我也希望删除孩子,但要注意的是,指针可能为零,因此在尝试删除之前,我需要检查是否有东西。

[I'm new to Javascript so maybe that's also part of the problem] [我是Java语言的新手,所以也许这也是问题的一部分]

Parse.Cloud.beforeDelete("MyModel", function(request) {
    if request.has(request.object.colors) {
      color = request.object.colors;
      Parse.Object.destroyAll(color, {
              success: function() {},
              error: function(error) {
                console.error("Error deleting related color " + error.code + ": " + error.message);
              }
            });
    }

    if request.has(request.object.goal) {
      goal = request.object.goal;
      Parse.Object.destroyAll(goal, {
          success: function() {},
          error: function(error) {
            console.error("Error deleting related goal " + error.code + ": " + error.message);
          }
        });
  }
});

Lets break this into smaller functions and correct a couple problems in the OP code along the way. 让我们将其分解为较小的功能,并一路纠正OP代码中的几个问题。 It's very helpful to reduce things to smaller, promise-returning functions keep the code modular and the concurrency manageable. 将事物简化为较小的,返回承诺的功能,这对保持代码模块化和并发性可管理非常有帮助。

EDIT Generally, it's preferred to use pointers to relate objects. 编辑通常,最好使用指针来关联对象。 Here's a general purpose function to delete an object related via pointer: 这是一个通用功能,用于删除通过指针关联的对象:

function deleteRelatedPointer(myModel, pointerName) {
    var pointer = myModel.get(pointerName);
    if (pointer) {
        return pointer.fetch().then(function(relatedObject) {
            return relatedObject.destroy();
        });
    } else {
        return null;
    }
}

Some authors relate objects via a string column containing the id of the related object. 一些作者通过包含相关对象ID的字符串列来关联对象。 Here's the equivalent function to delete an object related by id: 这是删除与id相关的对象的等效函数:

function deleteRelatedId(myModel, columnName, relatedClass) {
    var relatedId = myModel.get(columnName);
    if (relatedId) {
        var query = new Parse.Query(relatedClass);
        return query.get(relatedId).then(function(relatedObject) {
            return relatedObject.destroy();
        });
    } else {
        return null;
    }
}

Now, the beforeDelete method is easy to write and understand. 现在, beforeDelete方法易于编写和理解。 (Assuming the relationships via pointers): (通过指针假定关系):

Parse.Cloud.beforeDelete("MyModel", function(request, response) {
    var myModel = request.object;
    deleteRelatedPointer(myModel, "colors").then(function() {
        return deleteRelatedPointer(myModel , "goal");
    }).then(function() {
        response.success();
    }, function(error) {
        response.error(error);
    });
}

The other important thing to notice is that the before hook takes a response object and is required to invoke success / error on that object after the related tasks are complete. 另一个要注意的重要事项是,before挂钩接受一个响应对象,并且在相关任务完成需要调用该对象上的成功/错误。

All of this hinges on promises, which are necessary and immensely useful. 所有这些都取决于承诺,这​​是必要且非常有用的。 Read about parse's implementation of them here . 在此处了解parse的实现

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

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