0

I have the code below. Basically I have 3 nested parse queries. One is getting a number of "followers" and for each follower I am getting a number of "ideas" and for each idea I would like to get that idea creator's name (a user in the user table).

The first two nested queries work but then when i try to get the name of the user (the creator of the idea), that last nested query DOES NOT execute in order. That query is skipped, and then it is executed later in the code. Why is this happening please?

var iMax = 20;
                var jMax = 10;
                var IdeaList = new Array();
                var IdeaListCounter = 0;
                var myuser = Parse.User.current();
                var Followers = new Parse.Query("Followers");
                Followers.equalTo("Source_User",{__type: "Pointer",className: "_User",objectId: myuser.id});

                Followers.find({
                    success: function(results) {
                        for (var i = 0; i < results.length; i++) { 
                            var object = results[i];

                            var Ideas = new Parse.Query("Ideas");
                            Ideas.equalTo("objectId_User", {__type: "Pointer",className: "_User",objectId: object.get('Destination_User').id});

                            Ideas.find({
                                success: function(results2) {

                                    for (i=0;i<iMax;i++) {
                                        IdeaList[i]=new Array();
                                            for (j=0;j<jMax;j++) {
                                                IdeaList[i][j]=0;
                                            }
                                    }

                                    for (var j = 0; j < results2.length; j++) { 
                                        var object2 = results2[j];
                                        var ideausername2 = "";
                                        IdeaListCounter++;

                                        var ideausername = new Parse.Query("User");
                                        ideausername.equalTo("objectId",object2.get('objectId_User').id);

                                        ideausername.first({
                                            success: function(ideausernameresult) {
                                                ideausername2 = ideausernameresult.get("name");
                                            }
                                        });

                                        IdeaList[IdeaListCounter,0] = object2.get('objectId_User').id + " " + ideausername2; //sourceuser
                                        IdeaList[IdeaListCounter,1] = object2.get('IdeaText'); //text
                                        IdeaList[IdeaListCounter,2] = object2.get('IdeaImage'); //image
                                        IdeaList[IdeaListCounter,3] = object2.get('IdeaLikes'); //likes
                                        IdeaList[IdeaListCounter,4] = object2.get('IdeaReport'); //reports

1 Answer 1

0

Your nested query is asynchronous.

Check out the answer at the following for guidance: Nested queries using javascript in cloud code (Parse.com)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.