破解云代码afterSave被触发两次

时间:2015-12-14 03:37:20

标签: parse-platform cloud-code

我在Parse云代码上有一个消息。我已经对表进行了一次更改,但是后退钩子触发了两次

第一次更新(实际更新)

Input: {"object":{"addedBy":"tt","albumTitle":"ggh","approved":true,
"createdAt":"2015-12-14T03:07:27.253Z","image":
{"__type":"File","name":"tfss-e26ec608-5a0b-46a2-91db-33590139c4b3-
newImage.jpg","url":"http://files.parsetfss.com/0accbdba-e3f2-493d-
ac70-1c6bccc367b9/tfss-e26ec608-5a0b-46a2-91db-33590139c4b3-
newImage.jpg"},"objectId":"p4pLO70gQY","updatedAt":"2015-12-
14T03:07:37.733Z"}}

第二次更新(不应该发生)

Input: {"object":{"addedBy":"tt","albumTitle":"ggh","approved":true,
"createdAt":"2015-12-14T03:07:27.253Z","image":
{"__type":"File","name":"tfss-e26ec608-5a0b-46a2-91db-33590139c4b3-
newImage.jpg","url":"http://files.parsetfss.com/0accbdba-e3f2-493d-
ac70-1c6bccc367b9/tfss-e26ec608-5a0b-46a2-91db-33590139c4b3-
newImage.jpg"},"objectId":"p4pLO70gQY","updatedAt":"2015-12-
14T03:07:38.038Z"}}

两次更新之间的唯一区别是updatedAt时间。即使更新时间发生变化,是否会触发后退?

如何避免第二次执行死亡?

这是我的云代码:

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

    var albumNameA = "";
    var publicUser = "";
    var albumOwner = "";
    var photoUniqueId = "";
    var isApproved = "";

    albumNameA = request.object.get("albumTitle");
    publicUser = request.object.get("addedBy");
    photoUniqueId = request.object.id;
    isApproved = request.object.get("approved");

    if (request.object.get("createdAt").getTime() == request.object.get("updatedAt").getTime()) {
        var getAlbumOwner = new Parse.Object.extend("Albums");
        var q3 = new Parse.Query(getAlbumOwner);
        q3.equalTo("title", albumNameA);
        q3.first({
            success: function (results) {
                console.log("Checking for Creator name " + results.get("creatorName"));
                albumOwner = results.get("creatorName");
                console.log("Uploading a Photo Final " + albumNameA + "-by-" + publicUser + "--ownedby--" + albumOwner);
                console.log("Uploading a Photo" + albumNameA + "-by-" + publicUser + "--" + photoUniqueId + "--" + isApproved);
                var install = new Parse.Object.extend("Installation");
                var q2 = new Parse.Query(install);
                q2.equalTo("privacy", privateUserNo);
                q2.equalTo("username", albumOwner);
                q2.find({
                    success: function (results) {
                        if (!isApproved) {
                            Parse.Push.send({
                                where: q2, // Set our Installation query
                                data: {
                                    alert: "New Photo uploaded by  " + publicUser + " ,waiting for approval"
                                }
                            }, {
                                success: function () {
                                    // Push was successful
                                    console.log("Push success");
                                },
                                error: function (error) {
                                    // Handle error
                                    console.log("Push error");
                                }
                            })
                        }
                    },
                    error: function (error) {
                        console.log("Error: " + error.code + " " + error.message);
                    }
                });
            },
            error: function (error) {
                console.log("Error: " + error.code + " " + error.message);
            }

        });
    } else if (!(request.object.get("createdAt").getTime() == request.object.get("updatedAt").getTime())) {
        console.log("all case scenarios");
        var getAlbumOwner1 = new Parse.Object.extend("Albums");
        var q6 = new Parse.Query(getAlbumOwner1);
        q6.equalTo("title", albumNameA);
        q6.first({
            success: function (results) {
                albumOwner = results.get("creatorName");
                var sendApproval = new Parse.Object.extend("Photos");
                var q4 = new Parse.Query(sendApproval);
                q4.descending("updatedAt");
                q4.first({
                    success: function (results) {
                        var objectIDNeeded = results.id;
                        var isChanged = results.get("approved");
                        var currentUpdateTime = results.get("updatedTime");
                        console.log("Your Photo,  " + publicUser + " ,has been approved by " + albumOwner);
                        var install = new Parse.Object.extend("Installation");
                        var q5 = new Parse.Query(install);
                        q5.equalTo("privacy", privateUserNo);
                        q5.equalTo("username", publicUser);
                        q5.find({
                            success: function (results) {
                                Parse.Push.send({
                                    where: q5, // Set our Installation query
                                    data: {
                                        alert: "Your Photo has been approved by " + albumOwner
                                    }
                                }, {
                                    success: function () {
                                        // Push was successful
                                        console.log("Push success");
                                    },
                                    error: function (error) {
                                        // Handle error
                                        console.log("Push error");
                                    }
                                })
                            },
                            error: function (error) {
                                console.log("Error: " + error.code + " " + error.message);
                            }
                        });
                    },
                    error: function (error) {
                        console.log("Error: " + error.code + " " + error.message);
                    }

                });
            },
            error: function (error) {
                console.log("Error: " + error.code + " " + error.message);
            }
        });
    }
});

0 个答案:

没有答案