0

我从来没有学过javascript,所以请多多包涵。

我有一个云功能,除了指示的部分(>>)之外,它都可以:

Parse.Cloud.define("acceptRequest", function(request, response) {
var user = request.user;
var requestUser;
var requestObject;
var requestId = request.params.objectId;

 var query = new Parse.Query(GameRequests);
query.get(requestId,{
    //Get GameRequest Object - requestObject
    success: function (object) {

        var requestObject = object;

        var sender = requestObject.get("from");
        var senderId = sender[0];

        var query = new Parse.Query(Parse.User);
        query.get(senderId, {
            //From GameRequest Object data, find sender of request "requestUser"
            success: function (userObject) {

                var requestUser = userObject;

                var requestUserList = requestUser.get("SENTrequests")
                var requestIndex = requestUserList.indexOf(requestId);

                if (requestIndex > -1) {
                            requestUserList.splice(requestIndex, 1);
                    requestUser.set("SENTrequests",requestUserList);
                     if (requestUserList.length = 1) {
                        user.unset("SENTrequests");
                            }
                                            }

                var userList = user.get("RCDrequests");
                var userIndex = userList.indexOf(requestId);

                if (userIndex > -1) {
                            userList.splice(userIndex, 1);
                    user.set("RCDrequests",userList);
                    if (userList.length = 1) {
                        user.unset("RCDrequests");
                            }
                        }

                    requestObject.destroy({
                        success: function(requestObject) {


                            requestUser.add("partners",user.id);
                            user.add("partners",requestUser.id); 

                            var firstmsg = new GameMessage();
                            var secondmsg = new GameMessage();

                            firstmsg.set("sender", user.id);
                            firstmsg.set("receiver", requestUser.id);
                            firstmsg.set("sent", 0);
                            firstmsg.set("received", 0);
                            firstmsg.set("receiverName", requestUser.getUsername());

                            secondmsg.set("sender", requestUser.id);
                            secondmsg.set("receiver", user.id);
                            secondmsg.set("sent", 0);
                            secondmsg.set("received", 0);
                            secondmsg.set("receiverName", user.getUsername());

                            Parse.Object.saveAll([requestUser, user, firstmsg, secondmsg], { useMasterKey: true },{
                         >>       success: function() {

                         >>       console.log("Saving messages again");
                         >>       firstmsg.set("otherside", secondmsg.id);
                         >>       secondmsg.set("otherside", firstmsg.id);
                         >>          
                         >>       Parse.Object.saveAll([firstmsg, secondmsg]);
                                },
                                error: function(error) {
                                }

                            });

                            response.success("Successful");
                        },

                        error: function(requestObject, error){
                        }
                 });

            },

            error: function (userObject,error) {

            }
        });

    },

    error: function (object,error) {
        response.error("Error");
    }
  });

});

它应该保存两条消息的 objectId,因此每个消息都有对另一个的引用。
是什么导致了这个问题,我该如何解决?

谢谢

4

1 回答 1

0

我认为您在保存完成之前发送了您的 response.success("Successful") 。将您的响应移动到保存的成功处理程序。

你应该看看promises 部分。您将不需要当前拥有的深层嵌套函数。

于 2014-08-29T13:22:49.530 回答