Parse platform 如何在Parse Cloude代码中使用多个查询编写beforeDelete
我需要在解析云代码中编写beforeDelete函数,包含多个查询 以下代码将删除所有注释,但不会删除图像 注意:Post对象有2列(关系和关系) 图像和注释是自定义类 e、 g 编辑 解析日志Parse platform 如何在Parse Cloude代码中使用多个查询编写beforeDelete,parse-platform,back4app,parse-cloud-code,Parse Platform,Back4app,Parse Cloud Code,我需要在解析云代码中编写beforeDelete函数,包含多个查询 以下代码将删除所有注释,但不会删除图像 注意:Post对象有2列(关系和关系) 图像和注释是自定义类 e、 g 编辑 解析日志 如何修复此问题以成功销毁其所有图像和注释我建议将代码重新组织为更小的可测试、承诺返回函数,类似这样的 function commentsForPost(postId) { const commentsQuery = new Parse.Query("Comment"); comments
如何修复此问题以成功销毁其所有图像和注释我建议将代码重新组织为更小的可测试、承诺返回函数,类似这样的
function commentsForPost(postId) {
const commentsQuery = new Parse.Query("Comment");
commentsQuery.equalTo("postId", postId);
return commentsQuery.find({ useMasterKey: true })
}
function destroy(objects) {
return Parse.Object.destroyAll(objects,{useMasterKey: true});
}
function imagesForPost(post) {
let images = post.relation("images");
return images.query().find({useMasterKey: true});
}
现在beforeDelete函数清晰简单
Parse.Cloud.beforeDelete("Post", function(request, response) {
// get the post's comments
let post = request.object;
return commentsForPost(post.id)
.then(comments => destroy(comments))
.then(() => imagesFromPost(post))
.then(images => destroy(images))
.then(() => response.success());
});
这是“Post”对象的beforeDelete,但您正在使用其objectId查询Post。您是否有一个单独的贴子对象,它也附加了图像?您说Post有两列,都是关系,但您要查询postId字段。也许第二个查询是针对一个单独的对象的?有了承诺,它们将在主线程继续运行时在单独的线程上运行,因此在这里,您总是在查询运行之前调用
response.success()
。如果将查询放入数组并使用Parse.Promise.when(查询),则可以看到相关错误(…
@JakeT.不,我正在使用postId进行评论查询。请发布答案好吗?我不明白你的意思,我需要了解你的设置。你正在进行post查询。第二个。在//删除其所有图像下,对象结构是什么?你有正确的类吗?另外,假设图像存储为Parse Files,您不能在文件字段上使用.include()
。该文件将只是其URL,没有数据。如果您需要访问查询中的数据,则必须迭代查询结果。哦,是的……我创建第二个查询(Post)只是为了包含图像(关系)……总体而言,我有一个包含图像(关系)的Post类.我有另一个名为Comment的类,它有postId(String)。我还有另一个名为Image的类,它有imageFile(File)。
function commentsForPost(postId) {
const commentsQuery = new Parse.Query("Comment");
commentsQuery.equalTo("postId", postId);
return commentsQuery.find({ useMasterKey: true })
}
function destroy(objects) {
return Parse.Object.destroyAll(objects,{useMasterKey: true});
}
function imagesForPost(post) {
let images = post.relation("images");
return images.query().find({useMasterKey: true});
}
Parse.Cloud.beforeDelete("Post", function(request, response) {
// get the post's comments
let post = request.object;
return commentsForPost(post.id)
.then(comments => destroy(comments))
.then(() => imagesFromPost(post))
.then(images => destroy(images))
.then(() => response.success());
});