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