Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

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 named the same thing. Any ideas?

function getMyFriends() {

    var relation = Parse.User.current().relation("peopleIFollow");
    relation.query().find({
    success: function(results) {
        // results is an array of Parse.Object
        var myFriendsArrayTemp=[];
        while(document.getElementById("datalist").hasChildNodes() )    {
        document.getElementById("datalist").removeChild(document.getElementById("datalist").lastChild);
                }
        for (i = 0; i < results.length; i++)    {
            var user=results[i]
            // console.log(user.getUsername())
            var relation2 = user.relation("peopleIFollow");
            // console.log(relation2)
            relation2.query().find({
            success: function(theirfriends) {
                // results is an array of Parse.Object



                for (z = 0; z < theirfriends.length; z++)   {
                    var personTheyFollow=theirfriends[z];

                    if ( personTheyFollow.getUsername() == Parse.User.current().getUsername() ) {
                        myFriendsArrayTemp.push(user.getUsername())
                        console.log(user.getUsername())
                        var datalist=document.getElementById("datalist")
                        var option=document.createElement('option')
                        option.value=user.get("name");
                        datalist.appendChild(option)
                    }
                }

            }, 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();

        }
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

 function getMyFriends() {
    // query all the people I follow
    var relation = Parse.User.current().relation("peopleIFollow");
    relation.query().find({
    success: function(results) {

        //create an array to hold all confirmed friends
        var myFriendsArrayTemp=[];

      //clear some on page display
        while(document.getElementById("datalist").hasChildNodes() ){
            document.getElementById("datalist").removeChild(document.getElementById("datalist").lastChild);
        }

        // loop through each of the people I follow
        for (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) { // IIFE
                  return function(results) {

                    //loop through the people they follow to see if in in that list
                    for (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();

        }
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...