gpt4 book ai didi

javascript - Parse.com 云代码 : function called simultaneously by 2 users

转载 作者:行者123 更新时间:2023-11-28 07:21:39 24 4
gpt4 key购买 nike

我正在审查为我的应用程序编写的所有云代码,并且我已经发现了一些需要纠正的内容。我有一个充满用户和“ session ”对象的解析数据库。每个用户都可以创建或查看并接受 session 。当用户想要接受 session 时,将调用下一个函数。如果所需人数等于接受 session 并确认的人数,否则该 session 仍可供其他用户使用。今天我尝试同时接受 2 个客户的 session ,但 session 没有得到确认。当然,所需的人数是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//开始通话1,partnersObjectId = [](空数组)

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

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

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

{call 1} meetToAccept.get("participantsObjectId").length 检查返回 1//participantsObjectId = [user2]

{call 2} meetToAccept.get("participantsObjectId").length 检查返回 1

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

{call 2} Parse.Object.saveAll(changedObjects)//结果partitionsObjectId = [user2] 覆盖partnersObjectId = [user1]

另外,关于您的代码的评论:如果您在代码中留出空间,它会变得更具可读性。这样它就不会那么稠密。另外,我建议您在每个“然后”处对“然后”的作用发表评论。

关于javascript - Parse.com 云代码 : function called simultaneously by 2 users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30243176/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com