解析beforeSave()云代码功能不起作用

时间:2015-01-26 22:58:55

标签: javascript parse-platform cloud-code

我添加了一个无法正常工作的beforeSave()Parse云函数。

这是背景: 我有一个名为 UserRewards 的类,其中包含 ParseRelation 类型的用户列。它由1个关系组成,它具有 ParseUser 类型的单个对象。我有一个新列 pUser ,我想在此函数中设置。它会查找用户关系,在其中找到 ParseUser ,并将 pUser 设置为该用户。

这是我的代码

Parse.Cloud.beforeSave("UserRewards", function(request, response) {
    var relation = request.object.relation("user");
    relation.query().find({
            success: function(list) {
                console.log("looked up relation");
                if (list.length > 0) {
                    var pUser = list[0];
                    request.object.set("pUser", pUser);
                    console.log("updated pUser");
                }
            },
                error: function(error) {
                console.error("unable to lookup relation");
            }
    });
    response.success();

});

当我更新 UserRewards 行时,正在调用此函数。但它什么也没做 - 没有任何控制台或错误记录。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的函数在查询完成之前运行response.success(),从而取消它。尝试...

Parse.Cloud.beforeSave("UserRewards", function(request, response) {
    var relation = request.object.relation("user");
    relation.query().find({
            success: function(list) {
                console.log("looked up relation");
                if (list.length > 0) {
                    var pUser = list[0];
                    request.object.set("pUser", pUser);
                    console.log("updated pUser");
                }
                response.success();
            },
                error: function(error) {
                console.error("unable to lookup relation");
                response.error(error);
            }
    });
});

或者,更容易看到承诺...

Parse.Cloud.beforeSave("UserRewards", function(request, response) {
    var relation = request.object.relation("user");
    relation.query().find().then(function(list) {
        console.log("looked up relation");
        if (list.length > 0) {
            var pUser = list[0];
            request.object.set("pUser", pUser);
            console.log("updated pUser");
        }
        response.success();
    }, function(error) {
        console.error("unable to lookup relation");
        response.error(error);
    });
});

编辑我认为我理解后续问题是要求将指针属性与关系属性同步,假设在保存之前只设置了其中一个属性。这可以通过检测关系为空来完成。如果是,再做一个异步步骤来获取指针...

Parse.Cloud.beforeSave("UserRewards", function(request, response) {
    var relation = request.object.relation("user");
    relation.query().find().then(function(list) {
        console.log("looked up relation");
        if (list.length > 0) {
            var pUser = list[0];
            request.object.set("pUser", pUser);
            console.log("updated pUser");
            return Parse.Promise.as();  // we're done, this will pass nothing to the next step
        } else {  // the relation is empty, so the user pointer must be set
            var pointer = request.object.get("pUser");
            return pointer.fetch();    // this will pass the pUser to the next step
        }
    }).then(function(pUser) {
        if (pUser) {
            // must have found an empty relation in the last step.  add the user pointer to it
            request.object.relation.add(pUser);
        }
        response.success();
    }, function(error) {
        console.error("unable to lookup relation");
        response.error(error);
    });
});

(注意代码未经测试)。