使用 Parse Cloud 插入镜像对象

Insert mirror object using Parse Cloud

我有一个场景,我需要复制一个对象(在将它保存到 Parse 之后或之前)只更改一个字段。 问题是,我不知道如何在保存这个新对象时找到停止条件,并且一次又一次地调用 afterSave 回调。 我的对象:

{
        "createdAt": "2015-02-21T23:25:03.525Z",
        "creator": {
            "__type": "Pointer",
            "className": "_User",
            "objectId": "2k9OzzBrPr"
        },
        "date": {
            "__type": "Date",
            "iso": "2015-02-21T22:46:39.048Z"
        },
        "description": "Hdheha",
        "from": {
            "__type": "Pointer",
            "className": "_User",
            "objectId": "Sd9B1XyZVL"
        },
        "has_accepted": false,
        "has_answered": false,
        "objectId": "YQCWRo0j2V",
        "status": 0,
        "to": {
            "__type": "Pointer",
            "className": "_User",
            "objectId": "2k9OzzBrPr"
        },
        "updatedAt": "2015-02-21T23:25:03.525Z",
        "value": 2.3499999046325684
    }

我的(试用)服务器代码:

function saveMirrorDebit(request) {
    var toUser = request.object.get("to");
    var fromUser = request.object.get("from");
    var invertedDebit = request.object;

    var Debit = Parse.Object.extend("Debit");
    var query = new Parse.Query(Debit);
    query.equalTo("parent_debit", {
        __type : "Pointer",
        className : "Debit",
        objectId : invertedDebit.id
    });

    query.find({
        success : function (debit) {
            if (debit) {
                console.log('debito nao nulo');
            } else {
                console.log('debito nulo, criando o espelho invertido');

                var newDebit = new Debit();

                newDebit.set("creator", invertedDebit.get("creator"));
                newDebit.set("from", toUser);
                newDebit.set("to", fromUser);
                newDebit.set("value", -invertedDebit.get("value"));
                newDebit.set("parent_debit", {
                    __type : "Pointer",
                    className : "Debit",
                    objectId : invertedDebit.id
                });

                newDebit.save(null);
            }
        },
        error : function (error) {
            console.log(error);
        }
    });
}

保存后调用:

Parse.Cloud.afterSave("Debit", function (request) {
...
saveMirrorDebit(request);
...
}

我该如何解决这个问题?

谢谢

首先,我不建议您复制任何对象...您想要达到什么目的?

总之,在afterSave中,你可以达到你想要的。请注意 beforeSave 可以为您节省一个 API 电话。

Parse.Cloud.afterSave("Debit", function (request) {

    var debit = request.object;
    if (!(debit.has("duplicated"))){
        var Debit = Parse.Object.extend("Debit");
        var duplicated_debit = new Debit();
        duplicated_debit.set("creator", debit.get("creator"));
        duplicated_debit.set("date", debit.get("date"));
        duplicated_debit.set("from", debit.get("from"));
        // repeat as many times as needed, include your "change logic here"

        // that's where the magic happens
        duplicated_debit.set("duplicated",true);
        debit.set("duplicated",true);
        Parse.Object.saveAll([duplicated_debit,debit]);
    }
}