1

在下面的云代码中,我想获得 saveAll 函数的反馈,但是在解析日志页面中从我的客户端调用代码后,我只能看到:

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() 之后添加一些推送和另一个嵌套查询,但在执行/尝试之前我想知道这是否是正确的使用方法,或者我是否必须以不同的方式构建代码。我是 javascript 新手,老实说,我正在努力理解一些概念,比如承诺。任何帮助将非常感激。

4

1 回答 1

2

您的来电

Parse.Object.saveAll

是异步的,你调用

response.success("Meeting accepted")

异步调用后立即结束该方法的云代码运行。如果你只是更换

response.success("objects saved")

response.success("Meeting accepted")

你应该得到你想要的。


我没有看到你关于承诺的其余问题。您应该查看Parse 的有关链接承诺的文档,这正是您想要的。

本质上,这就是您想要做的事情:

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函数的末尾执行它并返回结果(它返回一个承诺)。继续添加.then函数,直到你完成所有你想做的工作,此时调用response.success.

于 2014-10-08T17:26:44.393 回答