问题描述
您好,我有一个可以将照片上传到服务器的应用程序.解析仅给我们20GB的存储空间,因此我不想超过该限制.我想要服务器,以便如果它已经存在3天了,它将删除文件.这就是代码
Hello I have an app that would upload photo's to the sever. Parse only gives us 20gb for storage and so I don't want to go over that limit. I want the server so that it would delete the files if it is 3 days old. So this is the code
Parse.Cloud.job('deleteOldPosts', function(request, status) {
// All access
Parse.Cloud.useMasterKey();
var today = new Date();
var days = 10;
var time = (days * 24 * 3600 * 1000);
var expirationDate = new Date(today.getTime() - (time));
var query = new Parse.Query('post');
query.lessThan('createdAt', expirationDate);
query.find().then(function (posts) {
Parse.Object.destroyAll(posts, {
success: function() {
status.success('All posts are removed.');
},
error: function(error) {
status.error('Error, posts are not removed.');
}
});
}, function (error) {});
});
但是,如果我使用此代码,它将从所有类中删除文件.我只希望此代码仅在一个类上运行.有可能这样做吗?
However If I use this code it would delete files from all classes. I just want this code to work on only one class. Is it possible to do so?
推荐答案
在云代码中删除对象时,请使用query.each
而不是query.find
来确保删除所有与查询匹配的对象.
When deleting objects in cloud code, use query.each
instead of query.find
to ensure that you delete all objects matching the query .
find
的查询限制为默认情况下返回的100个对象(如果使用limit
,则最多为1000个). 来源
find
has the query limitation of 100 objects returned by default (or up to 1000 if limit
is used). Source
下面是使用诺言链更新的代码,该诺言链在每个Post
对象上调用destroy
.当所有 销毁承诺均已完成时,将达到成功状态,如果任何销毁失败,则将达到错误状态.
Below is your updated code using a promise chain which calls destroy
on each Post
object. When all of the destroy promises have completed, the success status will be reached, and if any of the destroys fail then the error status will be reached.
Parse.Cloud.job('deleteOldPosts', function(request, status) {
// All access
Parse.Cloud.useMasterKey();
var today = new Date();
var days = 10;
var time = (days * 24 * 3600 * 1000);
var expirationDate = new Date(today.getTime() - (time));
var query = new Parse.Query('post');
query.lessThan('createdAt', expirationDate);
query.each(function(post) {
return post.destroy();
}).then(function() {
console.log("Delete job completed.");
status.success("Delete job completed.");
}, function(error) {
alert("Error: " + error.code + " " + error.message);
status.error("Error: " + error.code + " " + error.message);
});
});
这篇关于在特定类中经过一定时间后,删除inParse中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!