Parse.com Cloud Code: 函数被2个用户同时调用

Parse.com Cloud Code: function called simultaneously by 2 users

我正在审查我为我的应用程序编写的所有云代码,我已经发现了一些我需要更正的地方。我有一个充满用户和 "meeting" objects 的解析数据库。每个用户都可以创建或查看并接受会议。当用户想要接受会议时调用下一个函数。如果所需的人数等于接受会议的人数,则会议被确认,否则其他用户可以使用。今天我尝试同时接受来自 2 个客户的会议,但会议没有得到确认。当然,所需人数是 2。这就是我正在调用的函数。我该如何纠正这种行为?

// accept meeting 
Parse.Cloud.define("acceptMeeting", function(request, response) {
 Parse.Cloud.useMasterKey();
 var userAcceptingTheMeeting = request.user;
 var meetingId = request.params.meetingId;
 var meetingToAccept;
 var userCreatorOfMeeting;
 var changedObjects = [];
 var queryForMeeting = new Parse.Query("MeetingObject");
 queryForMeeting.get(meetingId).then(function(meeting) {
  meetingToAccept = meeting;
  userCreatorOfMeeting = meeting.get("user");
  // incrementing the "acceptedMeetings" number field on the database for the user that accepted the meeting
  userAcceptingTheMeeting.increment("acceptedMeetings", +1);
  changedObjects.push(userAcceptingTheMeeting);
  return changedObjects;
 }).then(function(changedObjects) {
  meetingToAccept.add("participantsObjectId", userAcceptingTheMeeting.id);
  meetingToAccept.add("participantsName", userAcceptingTheMeeting.get("username"));
  // if the length of the array containing all the participants is equal to the number required "meetingNumberOfPersons" then set "isAvailable" to false (the meeting is confirmed)
  if (meetingToAccept.get("participantsObjectId").length === meetingToAccept.get("meetingNumberOfPersons")) {
   meetingToAccept.set("isAvailable", false);
  }
  changedObjects.push(meetingToAccept);
  console.log(changedObjects.length);
  return changedObjects;
 }).then(function(saveChangedObjects) {
  return Parse.Object.saveAll(changedObjects); 
 }).then(function(push) {
  // check if the meeting is still available
  if (meetingToAccept.get("isAvailable") === true) {
   
   // the meeting is still available, send a notification only to the creator of the meeting
   // push to the creator of the meeting 
   
  } else if (meetingToAccept.get("isAvailable") === false) {
   
   // the meeting is confirmed, send notifications to everyone (creator and participants)
   // push to the creator of the meeting 
   
   var participantsArray = [];
   participantsArray = meetingToAccept.get("participantsObjectId");
   participantsArray.splice(participantsArray.indexOf(userAcceptingTheMeeting.id), 1 );
   for (var i = 0; i < participantsArray.length; i++) {
    var participant = new Parse.User({
     id: participantsArray[i]
    });
    
    // push to the other participants 
    
   }
  } 
  return changedObjects; 
 }).then(function(savedObjects) {
  if (meetingToAccept.get("isAvailable") === true) {
   response.success("unconfirmed");
  } else {
   response.success("confirmed");
  }
 }, function(error) {
  response.error("Failed to accept the meeting");
 });
});

我认为你应该在 .add() meetingToAccept.add("participantsObjectId", userAcceptingTheMeeting.id)

之后使用 .save()

考虑这一系列事件:

{call 1} acceptMeeting //开始call 1, participantsObjectId = [] (空数组)

{call 2} acceptMeeting // 开始 call 2,participantsObjectId = []

{call 1} meetingToAccept.add("participantsObjectId", userAcceptingTheMeeting.id)// participantsObjectId = [user1]

{call 2} meetingToAccept.add("participantsObjectId", userAcceptingTheMeeting.id) // t = 2 participantsObjectId = [user2]

{call 1} meetingToAccept.get("participantsObjectId").length check returns 1//participantsObjectId = [user2]

{呼叫 2} meetingToAccept.get("participantsObjectId").长度检查 returns 1

{call 1} Parse.Object.saveAll(changedObjects) // 结果为 participantsObjectId = [user1]

{call 2} Parse.Object.saveAll(changedObjects) // 结果为 participantsObjectId = [user2] OVERRIDING participantsObjectId = [user1]

此外,关于您的代码的评论:如果您 space 代码,它会变得更具可读性。这样它就不会那么密集。此外,我建议您在每个 "then" 上对 "then" 的作用发表评论。