Parse.com云代码:2个用户同时调用的函数

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

本文关键字:调用 函数 用户 2个 com 代码 Parse      更新时间:2023-09-26

我正在审查我为我的应用程序编写的所有云代码,我已经发现了一些我需要纠正的东西。我有一个充满用户和"会议"对象的解析数据库。每个用户都可以创建、查看和接受会议。当用户想要接受会议时,将调用下一个函数。如果所需的人数等于确认接受会议的人数,则其他用户仍可使用该会议。今天我尝试同时接受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()之后使用.save() meetingToAccept.add("participantsObjectId", userAcceptingTheMeeting.id)

考虑以下事件序列:

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

{call 2} acceptMeeting//开始呼叫2,participantsobjid = []

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

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

{call 1} meetingToAccept.get(" participantsobjid ")。1//participantsobjid = [user2]

{call 2} meetingToAccept.get(" participantsobjid ")。长度检查返回1

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

{call 2} Parse.Object.saveAll(changedObjects)//results in participantsObjectId = [user2] override participantsObjectId = [user1]

还有,关于你的代码的注释:如果你给代码加空格,它会变得更容易读。所以密度不是很大。另外,我建议你对每个"then"做一个评论。