0

I have an array of userIDs that I want to loop through, performing a lookup against a parse.com hosted database. I end up looping through the array of 5 elements but all I get are 5 of the same records outputted. Each outputted record is for the last item in the array. Example: if I have tom, dick, harry, jane, and ben in the array then I get 5 records output, all showing ben's information.

Can you take a look and tell me why this might be? Am I using the 'promise' feature properly?

Thank you!

    function LookupAndPrintFriends( friendList ) {

            var userQuery = new Parse.Query( Parse.User );
            var userobj;

            // Do something with the returned Parse.Object values
            var promise = Parse.Promise.as();

            alert( "got here and there are this many friends: " + friendList . length ); // returns 5 unique friend IDS
            _.each( friendList, function ( friendID ) {

                promise = promise.then( function () {

                    userQuery.equalTo( "objectId", friendID );
                    userQuery.first().then( function ( friendUserRecord ) {

                        alert( "i just did lookup on friend with id: " + friendID ); // shows a different friend ID each time through the loop

                        alert( "found him and his name is: " + friendUserRecord.get( "firstname" ) );  // always returns name of last friend in the array
                        userobj = friendUserRecord;
                        name = userobj.get( "firstname" ) + " " + userobj.get( "lastname" );
                        alert( "found this friend name: " + name );  // always shows name of last name in the array

                        myFriendsArray.push( name );

                    });

                    return;
                });

            }); //here
            return promise;
        }

1 Answer 1

2

You simply need to move the userQuery line inside the loop.

var userQuery = new Parse . Query( Parse . User );

Otherwise, you will end up nesting conditionals and requesting the first one alone.

I would also advise to use Promise.all with an array of all the promises instead of nesting your promises serially.

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

2 Comments

Thank you, Rilke. That was it. The code had worked at one time so I must have moved that declaration upward and not noticed the effect. Still seems odd that the variable that resides in the query does not change the nature of the query. You have to allocate a new query object for each query you run?
From the docs: > Parse.Query defines a query that is used to fetch Parse.Objects. That is to say, every method you call on the Query object (in your case .equalTo) is just defining the query before you send it up using the .first call.

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.