Using Parse And Javascript To Find Friends
In my program, if people follow you back you are friends. I'm trying to get my friends. I use the following function, and it sets up the correct number of users but they're all nam
Solution 1:
Your problem is that relation2.query().find()
is an asynchronous function as such the success
callback does not have access to the user whose relations are being quired.
To avoid this you can use an immediately invoked function expression to explicitly pass the user to the success callback.
This answer does a great job of explaining the issue
I cant test it, but the below should work for you:
functiongetMyFriends() {
// query all the people I followvar relation = Parse.User.current().relation("peopleIFollow");
relation.query().find({
success: function(results) {
//create an array to hold all confirmed friendsvar myFriendsArrayTemp=[];
//clear some on page displaywhile(document.getElementById("datalist").hasChildNodes() ){
document.getElementById("datalist").removeChild(document.getElementById("datalist").lastChild);
}
// loop through each of the people I followfor (i = 0; i < results.length; i++){
var followedUser=results[i] //use a more unique var for this than `user`var relation2 = followedUser.relation("peopleIFollow");
//get all the people this user follows
relation2.query().find({
success: (function(followedUser) { // IIFEreturnfunction(results) {
//loop through the people they follow to see if in in that listfor (z = 0; z < theirfriends.length; z++) {
var personTheyFollow=theirfriends[z];
if ( personTheyFollow.getUsername() == Parse.User.current().getUsername() ) {
// im in their list, friendship confirmed set some stuff
myFriendsArrayTemp.push(followedUser.getUsername())
console.log(followedUser.getUsername())
var datalist=document.getElementById("datalist")
var option=document.createElement('option')
option.value=user.get("name");
datalist.appendChild(option)
}
}
}
})(followedUser),
error: function(error) {
// error is an instance of Parse.Error.refreshTimedOut();
}
});
}
console.log("****")
console.log(myFriendsArrayTemp);
console.log("****")
}, error: function(error) {
// error is an instance of Parse.Error.refreshTimedOut();
}
});
}
Post a Comment for "Using Parse And Javascript To Find Friends"