在从客户端保存所有批次之前解析云代码

Parse Cloud Code Before Save on saveAll Batches from Client

我正在尝试找出处理来自 iOS 客户端的传入 saveAll 批处理的最佳方法。用户可以在应用程序中创建一些评论,如果他们离开应用程序或离开评论页面,它会批量保存所有评论对象。

在云代码中,我需要检查与评论关联的组织的支持票数,然后增加组织对象的计数,并使用票号保存评论。

假设当保存了 2 条评论时,它获取了工单号,但保存了两个具有相同工单号的评论。

例如:组织票数 100 我们的客户使用 saveAll 功能添加了 2 条评论。

云代码应该获取组织票数,然后用票数保存评论,递增组织票数并保存下一个等等。

这是我正在玩的一些示例代码。一切正常,但来自 saveAll 的评论都使用相同的组织票号保存。

  Parse.Cloud.beforeSave("Comment", function(request, response) {
// when a comment is added we get the current ticketNumber value
// from the org, and assign it to the new comment.
// after that we increment the counter on the org.

console.log('========================')
console.log(request.object.id) // empty
console.log(request.object.get('org'))
console.log('========================')

var orgId = request.object.get("org").id
// get the count from the org
query = new Parse.Query('Organization')
query.equalTo("objectId", orgId)
query.first({ useMasterKey: true }).then(function(object) {
    console.log('in query')
    // assign it as a variable.
    var ticketNumber = object.get('ticketNumber')
    console.log(ticketNumber)
    // atomic increment ticket number on org
    object.increment("ticketNumber")
    object.save(null, { useMasterKey: true });
    // save the ticketNumber to the comment
    request.object.set("ticketNumber", ticketNumber)
    response.success();      
  }, function(error) {
    response.error("something happened")
  });

});

当然,如果有一个对象进来,这就可以工作了..它只是从客户端保存多个对象..

Org 的保存和查询将异步进行,您的所有保存将同时进行,获取同一个组织,并在任何保存之前分配旧值通过.

而不是 iOS 客户端中的 saveAll,而是创建一个云代码函数,该函数采用要保存的评论数组。如果它们都属于同一个组织,您可以从第一个评论中获取一次,然后遍历您的评论,递增组织的 ticketNumber 并将其依次分配给每个对象,然后将所有内容(评论和组织)保存在一起。

如果评论不总是同一个组织,您可以先遍历您的评论以获取您需要的所有组织,将它们放在一个数组中并执行 fetchAll,然后遍历您的评论并适当地访问这些组织.这是一个好主意,因此在您有 20 张票的情况下,您将每次获取两个对象一次,而不是每次获取两个对象 10 次,2 个不同的组织各 10 个。

如果还需要访问工单,可以将org和工单分开保存,return在评论区保存saveAll的结果。

编辑:

在云代码中,你可以有这样的东西,假设所有的评论都有相同的组织:

Parse.Cloud.define("saveComments", function(request, response) {
    var comments = request.params.comments;
    if( !comments || comments.length == 0 ) return response.success(); //Handle case with no comments to save - Job done!
    var org = comments[0].get("org");
    // Since you don't need to include any Parse Objects, you can use fetch instead of query
    org.fetch({useMasterKey:true}).then(
        function(organization) {
            for( comment in comments ) {
                organization.increment("ticketNumber");
                comment.set("ticketNumber", organization.get("ticketNumber"));
            }
            return organization.save(null, {useMasterKey:true}).then(
                function(organization) {
                    return Parse.Object.saveAll(comments, {useMasterKey:true}); //Put this after saving the org, so the return value is the same as your current client save all call
                }
            );
        }
    ).then(
        function(success) { response.success(success); },
        function(error) { response.error(error); }
    );
});

然后在您的 iOS 客户端上,您可以不使用 saveAll,而是执行如下操作:

NSDictionary *params = @{"comments":commentsArray};
[PFCloud callFunctionInBackground:@"saveComments" withParams:params...];`