Parse.com云代码:2个用户同时调用的函数
Parse.com Cloud Code: function called simultaneously by 2 users
我正在审查我为我的应用程序编写的所有云代码,我已经发现了一些我需要纠正的东西。我有一个充满用户和"会议"对象的解析数据库。每个用户都可以创建、查看和接受会议。当用户想要接受会议时,将调用下一个函数。如果所需的人数等于确认接受会议的人数,则其他用户仍可使用该会议。今天我尝试同时接受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"做一个评论。
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量