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

时间:2015-05-14 17:06:01

标签: javascript parse-platform cloud-code

我正在审核我为我的应用编写的所有云代码,我已经找到了我需要纠正的内容。我有一个解析数据库,充满了用户和#34;会议"对象。每个用户都可以创建或查看并接受会议。当用户想要接受会议时,将调用下一个函数。如果所需人数等于接受会议的人数确认,则其他用户可以使用。今天我尝试同时接受来自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");
	});
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我认为您应该在.save() .add()

之后使用meetingToAccept.add("participantsObjectId", userAcceptingTheMeeting.id)

考虑这一系列事件:

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

{call 2} acceptMeeting //开始调用2,participantObjectId = []

{call 1} meetingToAccept.add(&#34; participantObjectId&#34;,userAcceptingTheMeeting.id)// participantObjectId = [user1]

{call 2} meetingToAccept.add(&#34; participantObjectId&#34;,userAcceptingTheMeeting.id)// t = 2 participantObjectId = [user2]

{call 1} meetingToAccept.get(&#34; participantObjectId&#34;)。length check return 1 // participantObjectId = [user2]

{call 2} meetingToAccept.get(&#34; participantObjectId&#34;)。length check returns 1

{call 1} Parse.Object.saveAll(changedObjects)//导致participantObjectId = [user1]

{call 2} Parse.Object.saveAll(changedObjects)//导致participantObjectId = [user2] OVERRIDING participantObjectId = [user1]

此外,关于您的代码的评论:如果您分隔代码,它会变得更具可读性。所以它不那么密集。另外,我建议每个&#34;然后&#34;你评论那是什么&#34;然后&#34;确实