Search code examples
parse-platformparse-cloud-code

Nested queries not running in Parse CloudCode


I am creating an iOS app and I am using Parse for my backend. The app essentially enables people to challenge friends to sleep more than x hours.

I am writing my processing function ("betWinner3") that should compute the winner of the bet/challenge. The details are: I am querying for a bet based on an objectId and I am comparing "betTime" and "hoursSlept".From this query, I also record the username under "better" and the username under "sleeper".If - else statements compute the three possible scenarios. Within each condition/scenario , I make two other queries to "User" class to update the points for the "better" and "sleeper".

When I run the code, my function runs successfully and it gets in the if-else statements but it does not wait for my two other queries to spit out their result.

Any help to solve the problem would be appreciated. I new to Javascript and Parse. Thanks

Code :

Parse.Cloud.define("betWinner3", function(request,response){

    var objectId=request.params.objectId;
    var query=new Parse.Query("betClass");
    query.equalTo("objectId",objectId);
    query.first({

        success:function(bet){

            var better=bet.get("better");
            var sleeper=bet.get("sleeper");
            console.log('start!!!');

            if (parseInt(bet.get("betTime")) < parseInt(bet.get("hoursSlept"))){
                console.log('sleeper wins');
                var queryUser= new Parse.Query(Parse.User);
                queryUser.equalTo("username",better);
                queryUser.first({
                    sucess:function(user){
                        var userPoints=parseInt(user.get("points"));
                        userPoints ++;
                        userBetPoints=userPoints.toString();
                        user.set("points",userBetPoints);
                        console.log("111111111111");
                        user.save();
                        response.success("Success Message");

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

                console.log('sle');
                var queryUser= new Parse.Query(Parse.User);
                queryUser.equalTo("username",sleeper);
                queryUser.first({
                    sucess:function(user){
                        var userPoints=parseInt(user.get("points"));
                        userPoints ++;
                        userBetPoints=userPoints.toString();
                        user.set("points",userBetPoints);
                        console.log("22222222222");
                        user.save();
                        response.success("Success Message");

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

            }

            else if (parseInt(bet.get("betTime")) === parseInt(bet.get("hoursSlept"))){
                console.log('tiessssss');
                var queryUser= new Parse.Query(Parse.User);
                queryUser.equalTo("username",better);
                queryUser.first({
                    sucess:function(user){
                        var userPoints=parseInt(user.get("points"));
                        userPoints ++;
                        userBetPoints=userPoints.toString();
                        user.set("points",userBetPoints);
                        console.log("333333333333");
                        user.save();
                        response.success("Success Message");

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

                console.log("lol");
                var queryUser= new Parse.Query(Parse.User);
                queryUser.equalTo("username",sleeper);
                queryUser.first({
                    sucess:function(user){
                        var userPoints=parseInt(user.get("points"));
                        userPoints +=0;
                        userBetPoints=userPoints.toString();
                        user.set("points",userBetPoints);
                        console.log("4444444");
                        user.save();
                        response.success("Success Message");

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

            }

            else {
                console.log("better wins");
                var queryUser= new Parse.Query(Parse.User);
                queryUser.equalTo("username",better);
                console.log("You are in");
                queryUser.first({
                    sucess:function(user){
                        var userPoints=parseInt(user.get("points"));
                        userPoints +=2;
                        userBetPoints=userPoints.toString();
                        user.set("points",userBetPoints);
                        console.log("55555555");
                        user.save();
                        console.log("aaaaaaa");
                        response.success("Success Message");

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

                var u=0;
                for(i=0; i<1000000;i++){
                u=i;
                }

                console.log("lmfao");
                var queryUser= new Parse.Query(Parse.User);
                queryUser.equalTo("username",sleeper);
                queryUser.first({
                    sucess:function(user){
                        var userPoints=parseInt(user.get("points"));
                        userPoints --;
                        userBetPoints=userPoints.toString();
                        user.set("points",userBetPoints);
                        console.log("6666666");
                        user.save();
                        console.log("bbbbbbbb");
                        response.success("Success Message");

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

            }


            console.log("wiiiiiiiiiiiiiii");
            response.success("Success Message %%%%%%%");

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

Solution

  • The problem is that the response.success() is getting called before your nested queries complete.

    Specifically, these lines get called as soon as your first query returns.

    console.log("wiiiiiiiiiiiiiii");
    response.success("Success Message %%%%%%%");
    

    As soon as success is called, the function ends. You can fix this by moving the success calls to exclusively within the nested queries. Or better yet, take a look at how Parse has implemented Promises They can really help organize your code.