android - 使用 Parse.Could.AfterSave 将关系添加到 ParseObject

标签 android parse-platform parse-cloud-code parse-android-sdk after-save

我只是在体验 parse sdk 并且我想在客户端添加的两个表之间存在关系时使用云代码来递增数字列(例如用户喜欢帖子),在解析文档中只有用于递增的代码基于保存对象的列,而不是使用 afterSave 的表之间的关系:

Parse.Cloud.afterSave("Comment", function(request) {
query = new Parse.Query("Post");
query.get(request.object.get("post").id, {
success: function(post) {
  post.increment("comments");
  post.save();
},
error: function(error) {
  console.error("Got an error " + error.code + " : " + error.message);
}
});
});

如何使用 afterSave 来改变两个表之间的关系?任何帮助将不胜感激。

最佳答案

我建议改用 beforeSave。这是我从我的一个项目中提取的示例代码。

Parse.Cloud.beforeSave(ClassConstants.Item.TYPE, function(request, response) {

    if(!request.object.isNew()) {
        // Retrieve the relationship information in json string format. 
        // RELATION_QUEUE here is simply a string "queue"
        var relQueueJsonStr = JSON.stringify(request.object.op(ClassConstants.Item.RELATION_QUEUE));

        if( relQueue !== undefined ) {
            var relQueue = JSON.parse(relQueueJsonStr);
            // Retrieve the operation being performed to this existing object.
            // The value will be "AddRelation" if a the relation "queue" is  
            // being added to this object
            var operation = relQueue.__op;
            if (operation == "AddRelation"){
                console.log("Relation queue is being added");
               // Do what you like here. In your case increment something

                response.success();
            } else {
            // Relation is being removed
                console.log("Relation queue is being removed");
                response.success();
            }
        } else {
            console.log("No queue relation.");
            response.success();
        }
    } else {
        response.success();
    }
});

关于android - 使用 Parse.Could.AfterSave 将关系添加到 ParseObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32294748/

相关文章:

ios - 使用云代码发送解析推送

Android getActionBar 与 getSupportActionBar?

javascript - 解析javascript for循环,创建多个不重复的对象

json - 如何在 Parse 中从 JSON 创建特定类型的对象

java - 在 Parse 中选择最多、第二多、第三...最近的用户

ios - 如何使用 nsdate 从解析中获取数据

swift - 使用 Swift Parse 和 Twilio 验证码总是无效

android - 删除 SD 卡上的文件夹

android - 错误: No resource found that matches the given name (at 'dial ogCornerRadius' with value '?android:attr/dialogCornerRadius' )

android - 当主应用程序未运行时显示通知的后台进程