Search code examples
javascriptparse-platformmbaas

Cloud code skipping section of code


I never learnt javascript so please bear with me.

I have a cloud function that does all but the indicated section (>>):

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");
    }
  });

});

It is supposed to save the two messages' objectIds so each has a reference to the other.
What is causing this problem and how can I fix it?

Thank you


Solution

  • I think your sending your response.success("Successful") before the save has been completed. Move your response to the success handler of the save.

    You should take a look at promises section. You will not need the deep nested functions you have currently.