解析:删除用户及其相关记录 [英] Parse: remove user and its related records

查看:51
本文介绍了解析:删除用户及其相关记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含实体的解析表.用户 - 默认类Commets - 带有指向 _User 实体的指针的类.

I have Parse table with entities. User - default class Commets - class with pointer to _User entity.

我需要从实体 User 中删除用户及其所有评论,位于评论实体中:

I need to delete user from entity User together with all its comments, located in Comments entity:

现在我有 JS Cloud 代码:

Right now I have JS Cloud code:

Parse.Cloud.define("deleteUser", function(request, response) {
                   var User = Parse.Object.extend("User");
                   var query = new Parse.Query(User);
                   var userID = request.params.userID;
                   query.get(userID,{
                             success: function(User) {
                             var message = 'success';

                             User.destroy({
                                          useMasterKey: true ,
                                            success:function() {
                                            response.success(message);
                                            return;
                                            },
                                            error:function(error) {
                                            response.error('Could not delete object '+ User.id);
                                            return;
                                            }
                                            });
                             },
                             error: function(object, error) {
                             var message = 'User could not found';
                             response.error(message);
                             }
                             });
                   });

它只是删除用户.我如何组合以从其他实体中删除用户的记录?

It's removing user only. How I can combine to remove also records from other entity by user?

感谢提前

推荐答案

正如 eth3lbert 在评论中指出的那样,您应该使用 afterDelete 钩子,该钩子在 User 对象被删除后被调用.您可以开始任何其他您想要的删除操作.

As eth3lbert pointed out in the comments, you should use an afterDelete hook that gets called after the User object has been deleted. You can kick off any other delete operations you want.

但是,有一个小问题,before* 和 after* 方法在挂钟时间 3 秒后被终止,这可能会导致不需要的结果,具体取决于您需要删除的数据量.

However, there is a little gotcha, the before* and after* methods get killed after 3 seconds of wall clock time which might lead to unwanted results depending on the amount of data that you need to delete.

最适合您的解决方案是设置后台作业(它们最多可以运行 15 分钟),安排它运行,比如说每天一次,然后在该作业中执行任何清理工作.

The best solution for you is to setup a background job (they can run for up to 15 minutes), schedule it to run, lets say once every day, and do any cleanup work in that job.

您可以为此创建一个简单的表,其中包含已删除用户的 objectId,每当您的 afterDelete 方法被调用时,您将删除的用户 ID 添加到该表中,然后您的后台作业会查询运行表并删除与其关联的内容.

You could create a simple table for that, that contains the objectIds of deleted users, whenever your afterDelete method gets called, you add the deleted users id into that table, your background job then queries that table on run and deletes the content that was associated with it.

这篇关于解析:删除用户及其相关记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆