【问题标题】:Using parse and javascript to find friends使用 parse 和 javascript 找朋友
【发布时间】:2015-02-24 17:59:38
【问题描述】:

在我的程序中,如果人们关注你,你就是朋友。我正在努力结交我的朋友。我使用以下函数,它设置了正确的用户数量,但它们都被命名为相同的东西。有什么想法吗?

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

        }
    });
}

【问题讨论】:

标签: javascript parse-platform


【解决方案1】:

您的问题是 relation2.query().find() 是一个异步函数,因此 success 回调无法访问正在查询其关系的用户。

为避免这种情况,您可以使用立即调用的函数表达式将用户显式传递给成功回调。

answer 很好地解释了这个问题

我无法对其进行测试,但以下内容应该适合您:

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

        }
    });
}

【讨论】:

  • personTheyFollow.getUsername() 只是我的用户名。这不是我想要的
  • 好吧,我再看一遍,我误会了一些东西
  • @BrentUnderwood 它不是真正的多线程,请查看stackoverflow.com/questions/8963209/…
猜你喜欢
  • 2015-10-21
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多