Parse.com Cloud Code:由2个用户同时调用的函数
我在审核我为我的应用程序编写的所有云代码,并且我已找到需要更正的内容。我有一个充满用户和“会议”对象的解析数据库。每个用户都可以创建或查看和接受会议。当用户想要接受会议时,下一个功能会被调用。如果所需人数等于接受会议的人数已确认,否则它可供其他用户使用。今天,我试着从两位客户那里同时接受一次会议,会议没有得到证实。当然,所需的人数是2.这是我打电话的功能。我怎样才能纠正这种行为?Parse.com Cloud Code:由2个用户同时调用的函数
// accept meeting Parse.Cloud.define("acceptMeeting", function(request, response) {
\t Parse.Cloud.useMasterKey();
\t var userAcceptingTheMeeting = request.user;
\t var meetingId = request.params.meetingId;
\t var meetingToAccept;
\t var userCreatorOfMeeting;
\t var changedObjects = [];
\t var queryForMeeting = new Parse.Query("MeetingObject");
\t queryForMeeting.get(meetingId).then(function(meeting) {
\t \t meetingToAccept = meeting;
\t \t userCreatorOfMeeting = meeting.get("user");
\t \t // incrementing the "acceptedMeetings" number field on the database for the user that accepted the meeting
\t \t userAcceptingTheMeeting.increment("acceptedMeetings", +1);
\t \t changedObjects.push(userAcceptingTheMeeting);
\t \t return changedObjects;
\t }).then(function(changedObjects) {
\t \t meetingToAccept.add("participantsObjectId", userAcceptingTheMeeting.id);
\t \t meetingToAccept.add("participantsName", userAcceptingTheMeeting.get("username"));
\t \t // 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)
\t \t if (meetingToAccept.get("participantsObjectId").length === meetingToAccept.get("meetingNumberOfPersons")) {
\t \t \t meetingToAccept.set("isAvailable", false);
\t \t }
\t \t changedObjects.push(meetingToAccept);
\t \t console.log(changedObjects.length);
\t \t return changedObjects;
\t }).then(function(saveChangedObjects) {
\t \t return Parse.Object.saveAll(changedObjects); \t
\t }).then(function(push) {
\t \t // check if the meeting is still available
\t \t if (meetingToAccept.get("isAvailable") === true) {
\t \t \t
\t \t \t // the meeting is still available, send a notification only to the creator of the meeting
\t \t \t // push to the creator of the meeting
\t \t \t
\t \t } else if (meetingToAccept.get("isAvailable") === false) {
\t \t \t
\t \t \t // the meeting is confirmed, send notifications to everyone (creator and participants)
\t \t \t // push to the creator of the meeting
\t \t \t
\t \t \t var participantsArray = [];
\t \t \t participantsArray = meetingToAccept.get("participantsObjectId");
\t \t \t participantsArray.splice(participantsArray.indexOf(userAcceptingTheMeeting.id), 1);
\t \t \t for (var i = 0; i < participantsArray.length; i++) {
\t \t \t \t var participant = new Parse.User({
\t \t \t \t \t id: participantsArray[i]
\t \t \t \t });
\t \t \t \t
\t \t \t \t // push to the other participants
\t \t \t \t
\t \t \t }
\t \t } \t
\t \t return changedObjects; \t
\t }).then(function(savedObjects) {
\t \t if (meetingToAccept.get("isAvailable") === true) {
\t \t \t response.success("unconfirmed");
\t \t } else {
\t \t \t response.success("confirmed");
\t \t }
\t }, function(error) {
\t \t response.error("Failed to accept the meeting");
\t });
});
回答:
,我认为你应该使用.save()
的.add()
meetingToAccept.add("participantsObjectId", userAcceptingTheMeeting.id)
后考虑这一系列事件:
{调用1} acceptMeeting //开始通话1,participantsObjectId = [](空阵列)
{调用2} acceptMeeting //开始呼叫2,participantsObjectId = []
{调用1} meetingToAccept.add( “participantsObjectId”,userAcceptingTheMeeting.id)// participantsObjectId = [USER1]
{调用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返回1
{调用1} Parse.Object.saveAll(changedObjects)//导致participantsObjectId = [USER1]
{调用2} Parse.Object.saveAll(changedObjects)//导致participantsObjectId = [USER2] OVERRIDING participaObjectId = [user1]
此外,有关您的代码的评论:如果您将代码放在空间中,它会变得更具可读性。所以它不那么密集。另外,我建议在每个“当时”你对“那时”做什么做评论。
以上是 Parse.com Cloud Code:由2个用户同时调用的函数 的全部内容, 来源链接: utcz.com/qa/265877.html