删除云代码中的指针属性

时间:2015-12-10 14:51:26

标签: javascript parse-platform cloud-code

从类“MyModel”中删除对象时,下面的代码试图通过指针属性删除与其相关的两个对象,一个称为“颜色”,一个称为“目标”。这些对象存在于数据中,但日志表示“找不到对象”错误。

代码来自this answer

Cloud Code:

function deleteMyModelPointer(myModel, pointerName, pointerClass) {
        var pointer = myModel.get(pointerName);
        if (pointer) {
            var query = new Parse.Query(pointerClass);
            return query.get(pointer).then(function(relatedObject) {
                return relatedObject.destroy();
            });
        } else {
            return null;
        }
    }

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

日志:

v14 before_delete triggered for MyModel for user wMgAGMOPNK: Input: {"object":{"ACL":{"wMgAGMOPNK":{"read":true,"write":true}},"colors":{"__type":"Pointer","className":"ColorModel","objectId":"Z3gEplJ0tq"},"counter":1,"createdAt":"2015-12-10T14:06:19.630Z","createdAtLocally":{"__type":"Date","iso":"2015-12-10T14:06:18.825Z"},"deletedLocally":false,"goal":{"__type":"Pointer","className":"Goal","objectId":"BkruZqhyJ7"},"lastModifiedAt":{"__type":"Date","iso":"2015-12-10T14:06:24.270Z"},"objectId":"LuobH2P8iz","resetValue":0,"stepBy":1,"title":"Ggggggg","updatedAt":"2015-12-10T14:06:24.670Z","user":{"__type":"Pointer","className":"_User","objectId":"wMgAGMOPNK"}}} Result: {"code":101,"message":"Object not found."}

但是两个指针仍然存在于对象浏览器和客户端。正如我所说,ACL被设置为所有3个对象的登录PFUser(具有读取,写入权限)。

1 个答案:

答案 0 :(得分:1)

问题是OP代码是为objectId属性而不是指针编写的。有了objectId,正确的操作是query.get()来获取相关对象,但对于指针,只需要fetch指针(并且不需要知道它的类) )。

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

来电方可以保持不变,只需删除第三个参数。