在下面的云代码中,我想获得saveAll函数的反馈,但在解析Logs页面中调用我客户端的代码后,我只能看到:
I2014-10-08T15:28:32.930Z] v249: Ran cloud function acceptMeetingBis for user dyGu143Xho with:
Input: {"meetingId":"bUSTGNhOer"}
Result: Meeting accepted
这是我的云代码:
Parse.Cloud.define("acceptMeetingBis", function(request, response) {
var userAcceptingTheMeeting = request.user;
var meetingId = request.params.meetingId;
var changedObjects = [];
var queryForMeeting = new Parse.Query("MeetingObject");
queryForMeeting.equalTo("objectId", meetingId);
queryForMeeting.first({
success: function(meeting) {
var userCreatorOfMeeting = meeting.get("user");
userAcceptingTheMeeting.increment("acceptedMeetings", +1);
changedObjects.push(userAcceptingTheMeeting);
meeting.add("participantsObjectId", userAcceptingTheMeeting.id);
if (meeting.get("participantsObjectId").length === meeting.get("meetingNumberOfPersons")) {
meeting.set("isAvailable", false);
}
changedObjects.push(meeting);
Parse.Object.saveAll(changedObjects, {
success: function(objects) {
console.log("Successfully saved objects"); //this line doesn't show up
response.success("objects saved");
},
error: function(error) {
// An error occurred while saving one of the objects.
response.error(error);
}
});
//future query and push notifications will go here
response.success("Meeting accepted");
},
error: function() {
response.error("Failed to accept the meeting");
}
});
});
我还需要在saveAll()之后添加一些push和另一个嵌套查询但是在做/尝试之前我想知道这是否是正确的使用方法或者我是否必须在不同的代码中构建代码办法。我是javascript的新手,说实话,我很难理解一些概念,比如承诺。任何帮助将不胜感激。
答案 0 :(得分:2)
致电
Parse.Object.saveAll
是异步的,你打电话给
response.success("Meeting accepted")
,这结束了该方法的云代码运行。如果你只是替换
response.success("objects saved")
与
response.success("Meeting accepted")
你应该得到你想要的东西。
我没有看到关于承诺的其余问题。你应该看看Parse's documentation on chaining promises,这就是你想要的。
基本上,这就是你想要做的事情:
Parse.Cloud.define("acceptMeetingBis", function(request, response) {
var userAcceptingTheMeeting = request.user;
var meetingId = request.params.meetingId;
var changedObjects = [];
var meetingToAccept;
var queryForMeeting = new Parse.Query("MeetingObject");
queryForMeeting.get(meetingId).then(function(meeting) {
meetingToAccept = meeting;
var userCreatorOfMeeting = meeting.get("user");
userAcceptingTheMeeting.increment("acceptedMeetings", +1);
return userAcceptingTheMeeting.save();
}).then(function(userWhoAcceptedMeetingNowSaved) {
meetingToAccept.add("participantsObjectId", userWhoAcceptedMeetingNowSaved.id);
if (meetingToAccept.get("participantsObjectId").length === meetingToAccept.get("meetingNumberOfPersons")) {
meetingToAccept.set("isAvailable", false);
}
return meetingToAccept.save();
}).then(function(savedMeeting) {
response.success("Meeting accepted");
}, function(error) {
response.error("Failed to accept the meeting");
});
});
对于您想要执行的每个异步操作,在其中一个.then
函数的末尾执行它并返回结果(它返回一个promise)。继续添加.then
功能,直到您完成所有要完成的工作,然后拨打response.success
。