解析afterSave函数被跳过

时间:2014-10-15 16:56:53

标签: javascript parse-platform cloud-code after-save

所以我有一个使用parse.com作为我的后端的消息传递应用程序。当我从应用程序发送消息时,它将它保存在Parse.com上的一个名为“NewMessages”的类中。然后在我的云代码中,我有一个专用于此类的afterSave函数,以便当一个新对象保存到“NewMessages”时,它会选择一个随机用户将其附加到消息并将其保存在一个名为“Inbox”的新类中。然后它从“NewMessages”中删除原始消息。

所以“NewMessages”类应该总是空的吗?但是当我很快发送一堆消息时,有些消息会被跳过。我该如何解决这个问题?

有没有比使用afterSave更好的方法来构建它?

function varReset(leanBody, leanSenderName, leanSenderId, randUsers){
   leanBody = "";
   leanSenderName = "";
   leanSenderId = "";
   randUsers = [];
   console.log("The variables were set");
}


Parse.Cloud.afterSave("Lean", function(leanBody, leanSenderName, leanSenderId, randUsers, request) {
  varReset(leanBody, leanSenderName, leanSenderId, randUsers);

  var query = new Parse.Query("NewMessages");
  query.first({
    success: function(results){
      leanBody = (results.get("MessageBody"));
      leanSenderName = (results.get("senderName"));
      leanSenderId = (results.get("senderId"));
      getUsers(leanBody, leanSenderName, leanSenderId);
      results.destroy({
        success: function(results){
          console.log("deleted");
        }, error: function(results, error){
        }
      });
    }, error: function(error){
    }

  });
});

  function getUsers(leanBody, leanSenderName, leanSenderId, response){

    var query = new Parse.Query(Parse.User);
    query.find({
        success: function(results){
            var users = [];
            console.log(leanBody);
            console.log(leanSenderName);

            //extract out user names from results
            for(var i = 0; i < results.length; ++i){
                users.push(results[i].id);
            }
            for(var i = 0; i < 3; ++i){
                var rand = users[Math.floor(Math.random() * users.length)];
                var index = users.indexOf(rand);
                users.splice(index, 1);
                randUsers.push(rand);
                }
            console.log("The random users are " + randUsers);
            sendMessage(leanBody, leanSenderName, leanSenderId, randUsers);
        }, error: function(error){
            response.error("Error");
        }
    });
  }

  function sendMessage(leanBody, leanSenderName, leanSenderId, randUsers){
    var Inbox = Parse.Object.extend("Inbox");

    for(var i = 0; i < 3; ++i){

      var inbox = new Inbox();
      inbox.set("messageBody", leanBody);
      inbox.set("senderName",  leanSenderName);
      inbox.set("senderId", leanSenderId);
      inbox.set("recipientId", randUsers[i]);
      console.log("leanBody = " + leanBody);
      console.log("leanSenderName = " + leanSenderName);
      console.log("leanSenderId = " + leanSenderId);
      console.log("recipient = " + randUsers[i]);

      inbox.save(null, {
        success: function(inbox) {
          // Execute any logic that should take place after the object is saved.
          alert('New object created with objectId: ' + inbox.id);
        },
        error: function(inbox, error) {
          // Execute any logic that should take place if the save fails.
          // error is a Parse.Error with an error code and message.
          alert('Failed to create new object, with error code: ' + error.message);
        }
      });
    }
  }

1 个答案:

答案 0 :(得分:1)

  1. 您检查了日志吗?您可能会遇到资源限制(https://parse.com/docs/cloud_code_guide#functions-resource)。如果即时性并不重要,那么可能值得研究设置一个每隔几分钟运行一次的后台作业并处理未发送的消息。也可以将两种方法结合起来:让afterSave函数尝试立即传递到收件箱,而后台工作定期获取剩余的任何NewMessages。不是最漂亮的解决方案,但至少你有更多的可靠性。 (你必须考虑竞争条件,尽管两者可能会尝试在同一个NewMessage上交付。)

  2. 关于更好的结构问题,如果两个类相同(或足够接近),是否可以只有一个Messages类?最初是&#34;到&#34;字段将为null,但在beforeSave函数上分配了一个随机收件人。这可能更快更整洁。

  3. 编辑:添加最初评论的第三个观察结果:

    我看到您在afterSave中使用Query.first()以找到要处理的NewMessage。潜在地,新的NewMessage可能在调用之后的时间和查询运行之间悄悄溜走。为什么不获取已保存的NewMessage的ID并在查询中使用它而不是first()

    query.get(request.object.id,...);

    这可确保afterSave中的代码处理为其调用的NewMessage,而不是最近保存的那个。