2

My Parse Javascript cloudcode is not working correctly. Basically all my objects have a corresponding "index/menindex & womenindex" number.

If both genders are selected- all items show up (as they should).

If just male or female is selected- it only shows items up to 122309 (womenindex) or 80811 (menindex). I've tried for weeks but cannot figure this one out.

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

var maxIndex = 124940;
var maxWomenIndex = 135354;
var maxMenIndex = 105357;

var arrayOfRandomNumbers = new Array();

var query = new Parse.Query("garmentsAPI");

    if (request.params.gender === "Female") {

        //arrayOfRandoms draws on womenIndex, gender set to female

        for (var k = 0; k < 40; k++) {

            var randomIndex =  Math.floor((Math.random()*maxWomenIndex)+1);

            arrayOfRandomNumbers.push(randomIndex);

                }
        query.containedIn("womenIndex", arrayOfRandomNumbers);
        query.notEqualTo ("viewers", request.params.user);


    }else if (request.params.gender === "Male") {

        for (var k = 0; k < 35; k++) {

            var randomIndex =  Math.floor((Math.random()*maxMenIndex)+1);

            arrayOfRandomNumbers.push(randomIndex);

                }

        query.containedIn("menIndex", arrayOfRandomNumbers);
        query.notEqualTo ("viewers", request.params.user);

        //arrayOfRandoms draws on menIndex, gender set to male

    }else{

        for (var k = 0; k < 35; k++) {

            var randomIndex =  Math.floor((Math.random()*maxIndex)+1);

            arrayOfRandomNumbers.push(randomIndex);

                }

        query.containedIn("index", arrayOfRandomNumbers);
        query.notEqualTo ("viewers", request.params.user);

    };


    if (request.params.gender !== "both") {
    query.equalTo ("gender", request.params.gender);
    };

    query.find({

    success: function(objectsInQuery){

        if (objectsInQuery.length > 15) {

            var arrayOSelectedObjects = new Array();

            for (var j = 0; j < 15; j++) { 

                arrayOSelectedObjects.push(objectsInQuery[j]);

            }

            response.success(arrayOSelectedObjects);

        } else{
                // var existingCount = objectsInQuery.count;
                var missingIndex = 15 - objectsInQuery.length;


                // missingIndex = missingIndex - objectsInQuery.count; 

                var query = new Parse.Query("garmentsAPI");
                if (request.params.gender !== "both") {
                query.equalTo ("gender", request.params.gender);
                };
                query.notEqualTo ("viewers", request.params.user);


                query.limit(missingIndex);
                // query.limit = 9 - objectsInQuery.count;


                query.find({

               success: function(objectsInQuery2){

                if ((objectsInQuery.length + objectsInQuery2.length) === 15) {


                    for (var l = 0; l < missingIndex; l++) { 

                        objectsInQuery.push(objectsInQuery2[l]);

                    }

                    response.success(objectsInQuery);


                } else {



                    //who knows


                    var maxIndex2 = 15000;

                    var arrayOfRandomNumbers2 = new Array();

                    for (var m = 0; m < 15; m++) {

                    var randomIndex =  Math.floor((Math.random()*maxIndex)+1);

                    // var randomIndex =  15;

                    arrayOfRandomNumbers2.push(randomIndex);

                    }


                    var query = new Parse.Query("garmentsAPI");
                    query.containedIn("index", arrayOfRandomNumbers2);
                    if (request.params.gender !== "both") {
                    query.equalTo ("gender", request.params.gender);
                    };
                    // query.notEqualTo ("viewers", request.params.user);


                    query.limit(15);

                    query.find ({


                        success: function (objectsInQuery3){

                            response.success (objectsInQuery3);
                        },

                        error:function() {

                            response.error("there was an error");
                        }


                    }); 


                };



                    },
                error:function() {

            response.error ("didn't work");

                }

            }

                );



        };



            },
        error:function() {

            response.error ("didn't work");

        }

        });

});

1 Answer 1

0

For starters, all the curly braces you've posted don't match up. Specifically, I can't find the closing brace for this if statement...

if (objectsInQuery.length > 15) {

It should be the first character of line 173, according to what you've posted...

as such...

                });



        };



            },
        error:function() {

            response.error ("didn't work");

        }

        });

});

vs. your post, as follows...

                );



        };



            },
        error:function() {

            response.error ("didn't work");

        }

        });

});
1
  • Sure- I added the closing brace for that statement.
    – Yian
    Aug 24, 2015 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.