javascript - Parse.com Cloud Code: function called simultaneously by 2 users -
i'm reviewing cloud code wrote app , found need correct. have parse data base filled users , "meeting" objects. every user can create or , accept meeting. next function gets called when user wants accept meeting. if required number of persons equal number of persons accepted meeting become confirmed, else stays available other users. today tried accepting meeting simultaneously 2 client , meeting didn't confirmed. of course required number of persons 2. function i'm calling. how can correct behavior?
// 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 "acceptedmeetings" number field on database user accepted 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 length of array containing participants equal number required "meetingnumberofpersons" set "isavailable" false (the meeting 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 meeting still available if (meetingtoaccept.get("isavailable") === true) { // meeting still available, send notification creator of meeting // push creator of meeting } else if (meetingtoaccept.get("isavailable") === false) { // meeting confirmed, send notifications (creator , participants) // push creator of meeting var participantsarray = []; participantsarray = meetingtoaccept.get("participantsobjectid"); participantsarray.splice(participantsarray.indexof(useracceptingthemeeting.id), 1 ); for (var = 0; < participantsarray.length; i++) { var participant = new parse.user({ id: participantsarray[i] }); // push other participants } } return changedobjects; }).then(function(savedobjects) { if (meetingtoaccept.get("isavailable") === true) { response.success("unconfirmed"); } else { response.success("confirmed"); } }, function(error) { response.error("failed accept meeting"); }); });
i think should using .save()
after .add()
meetingtoaccept.add("participantsobjectid", useracceptingthemeeting.id)
consider sequence of events:
{call 1} acceptmeeting //begin call 1, participantsobjectid = [] (empty array)
{call 2} acceptmeeting // begin 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]
{call 2} meetingtoaccept.get("participantsobjectid").length check returns 1
{call 1} parse.object.saveall(changedobjects) // results in participantsobjectid = [user1]
{call 2} parse.object.saveall(changedobjects) // results in participantsobjectid = [user2] overriding participantsobjectid = [user1]
also, comment code: becomes more readable if space code. it's not dense. also, suggest @ each "then" make comment "then" does.
Comments
Post a Comment