解析:从云代码更新对象 [英] Parse: Update Object from Cloud Code

查看:68
本文介绍了解析:从云代码更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个后台作业,每分钟运行一次.其目的是删除数据库中的旧数据或过时的数据.在这种情况下,用户提交带有附加时间戳的时间,并且这两个值存储在数组timestimestamp中.所以我写了我的云代码,通过时间戳检查时间是否大于一个小时.如果是这样,请从数据库中删除它们.但是,这就是我遇到的问题.这是我的云代码,因此您可以查看发生了什么:

I have created a background job to run every minute. The purpose of it is to delete old or outdated data within the database. In this case, the user submits a time which has an attached timestamp to it, and the two values are stored in arrays times and timestamp. So I wrote my cloud code to check if the times are older than an hour via their timestamps. If so, delete them from the database. However, that's the part I'm having trouble with. Here's my cloud code so you can see what's going on:

Parse.Cloud.job("expireTimes", function(request, response){
Parse.Cloud.useMasterKey();
var currentTime = new Date().getTime() / 1000;
var query = new Parse.Query("Wait");
query.find().then(function(results) {
    var times = (results.length>0)? results[0].get("times") : "n/a";
    var timestamps = (results.length>0)? results[0].get("timestamp") : "n/a";
    console.log("Got " + results.length + " Times: " + times);
    console.log("Got " + results.length + " Timestamps: " + timestamps);
    for (var i = 0; i < timestamps.length; i++) {
        if (currentTime >= timestamps[i] + 3600) {
            // Delete old wait times
            timestamps.splice(i, 1);
            times.splice(i, 1);
            i--;
        } else {
            break;
        }
    };

    response.success("success");
}, function(error) {
    response.error(error);
});
})

如何更新数据库并删除数组中的值.因为当用户提交时间时,它只会添加到times数组中.好吧,如果我每分钟都有后台工作,那么某些条目不会超过一个小时.我想保留这些,但要摆脱其他.

How do I update the database and delete the values within the array. Because when a user submits the time, it just adds to the times array. Well, if I have the background job going every minute, some entries aren't going to be over an hour old. I want to keep those, but get rid of the other ones.

有什么想法吗?任何帮助深表感谢. 谢谢

Any ideas? Any help is much appreciated. Thank you

推荐答案

答案的基本部分是可以使用set()设置对象属性,并使用save()保存对象.这是一个简单的例子……

The essential part of the answer is that object properties can be set with set() and objects are saved with save(). Here's a quick example of just that...

    // say object is some object that's been retrieved from the database
    var times = object.get("times");
    var timestamps = object.get("timestamp");  // would be better to make the array name plural in the database

    // do whatever you want to the arrays here, then ...

    object.set("times", times);
    object.set("timestamp", timestamps);
    return results[0].save();
}).then(function(result) {
        response.success("success");
}, function(error) {
        response.error(error);
});

但是我知道您确实想对许多Wait对象执行此操作,因此我们将需要更强大的设计.让我们开始养成构建执行逻辑工作块的函数的习惯.这是修复给定Wait对象的那些数组的方法...

But I understand you really want to do this for many Wait objects, so we're going to need a stronger design. Let's start getting into the habit of building functions that do logical chunks of work. Here's one that fixes those arrays for a given Wait object...

// delete parts of the waitObject time arrays prior to currentTime
function removeOldWaitTimes(waitObject, currentTime) {
    var times = waitObject.get("times");
    var timestamps = waitObject.get("timestamp");
    for (var i = 0; i < timestamps.length; i++) {
        if (currentTime >= timestamps[i] + 3600) {
            // Delete old wait times
            timestamps.splice(i, 1);
            times.splice(i, 1);
            i--;
        } else {
            break;
        }
    };
    waitObject.set("times", times);
    waitObject.set("timestamp", timestamps);
}

对于每个找到的Wait对象,我们需要循环调用此函数.完成后,我们需要保存所有已更改的对象...

We need to call this in a loop, for each Wait object found. When we're done, we need to save all of the objects that were changed...

Parse.Cloud.job("expireTimes", function(request, response){
    Parse.Cloud.useMasterKey();
    var currentTime = new Date().getTime() / 1000;
    var query = new Parse.Query("Wait");
    query.find().then(function(results) {
        for (var i=0; i<results.length; i++) {
            removeOldWaitTimes(results[i], currentTime);
        }
        return Parse.Object.saveAll(results);
    }).then(function(result) {
        response.success("success");
    }, function(error) {
        response.error(error);
    });
});

请注意,随着Wait类中对象的数量增加,作业可能会变得太长,或者查询(可以将其设置为返回的最大限制为1k个对象)可能会错过结果.如果有风险,我们将需要改进查询. (例如,如果作业每小时运行一次,则没有理由检索一个多小时前更新的等待"对象).

Please note that as the number of objects in the Wait class grows large, the job might get too long or the query (which can be set to a max limit of 1k objects returned) might miss results. We'll need to improve the query if that becomes a risk. (e.g. if the job runs every hour, there's no reason to retrieve Wait objects that were updated more than an hour ago).

编辑说您要像上面一样更新其中的一些内容,并销毁其他内容.您需要履行两个诺言才能做到这一点……

EDIT say you want to update some as above, and destroy others. You'll need to handle two promises to do that...

Parse.Cloud.job("expireTimes", function(request, response){
    Parse.Cloud.useMasterKey();
    var destroyThese = [];
    var currentTime = new Date().getTime() / 1000;
    var query = new Parse.Query("Wait");
    query.find().then(function(results) {
        var updateThese = [];
        for (var i=0; i<results.length; i++) {
            var result = results[i];
            if (result.get("times").length > 0) {
                removeOldWaitTimes(result, currentTime);
                updateThese.push(result);
            } else {
                destroyThese.push(result);
            }
        }
        return Parse.Object.saveAll(updateThese);
    }).then(function(result) {
        return Parse.Object.destroyAll(destroyThese);
    }).then(function(result) {
        response.success("success");
    }, function(error) {
        response.error(error);
    });
});

我们将destroyThese数组放置在封闭范围内,以便可用于承诺链的下一步.

We place destroyThese array in the enclosing scope so that it is available to the next step in the chain of promises.

这篇关于解析:从云代码更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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