在Parse中检索用户名而不是ObjectID

时间:2015-03-03 01:22:18

标签: javascript jquery html angularjs parse-platform

使用angular和Parse for JavaScript,当管理员输入用户的objectid时,我有一个搜索字段,点击提交后,用户的详细信息将填入各自的字段中。我的问题是,我不想使用objectid作为参考点,而是想使用用户名。在不输入objectid的情况下,他会输入用户名,并填充各自的信息。

下面是我如何根据提供的objectid显示用户信息(想要将其更改为用户名)。 脚本

//To search and display user information
   $scope.userIdChanged = function () {
       $scope.loading = true;

   // now access $scope.userId here
   var query = new Parse.Query(Parse.User);

   query.get($scope.userId, {
       success: function(userInfo) {
           // The object was retrieved successfully.
           var address = userInfo.get("address");
           $scope.address = address;

           var email = userInfo.get("username");
           $scope.email = email;

            var fullName = userInfo.get("fullName");
           $scope.fullName=  fullName;

               var number = userInfo.get("phoneNumber");
           $scope.number=  number;

            var mailingAddress = userInfo.get("mailingAddress");
           $scope.mailingAddress =  mailingAddress;

               var plan = userInfo.get("plan");
           $scope.plan =  plan;


                var phone = userInfo.get("Phone");
           $scope.phone = 'Phone: ' + phone;

             var scanURL = '<a href="scan.html">Scan</a>';
           $scope.scanURL = scanURL;
                      $scope.loading = false;


       },
       error: function(object, error) {
           // The object was not retrieved successfully.
           // error is a Parse.Error with an error code and message.
                      $scope.loading = false;

       }
   });

HTML

<form>
<label>
<input type="text" placeholder="Search username to begin" class="btn btn-danger" id="searchBar" ng-model="userId"></input>
</label>
<button class="btn btn-danger" id="searchButton" ng-click="userIdChanged()">SEARCH</button> </form>
    <h4 id="messageSent"></h4> 
<form>

更新

//To search and display user information
   $scope.userIdChanged = function () {
       $scope.loading = true;

       // now access $scope.userId here
       var query = new Parse.Query(Parse.User);

       query.equalTo("username", $scope.userId); 


       query.find({ success: function(userInfo) {
               // The object was retrieved successfully.
               var address = userInfo.get("address");
               $scope.address = address;

               var email = userInfo.get("username");
               $scope.email = email;

                var fullName = userInfo.get("fullName");
               $scope.fullName=  fullName;

                   var number = userInfo.get("phoneNumber");
               $scope.number=  number;

                var mailingAddress = userInfo.get("mailingAddress");
               $scope.mailingAddress =  mailingAddress;

                   var plan = userInfo.get("plan");
               $scope.plan =  plan;


                    var phone = userInfo.get("Phone");
               $scope.phone = 'Phone: ' + phone;

                 var scanURL = '<a href="scan.html">Scan</a>';
               $scope.scanURL = scanURL;
                          $scope.loading = false;


           },
           error: function(object, error) {
               // The object was not retrieved successfully.
               // error is a Parse.Error with an error code and message.
                          $scope.loading = false;

           }
       });

1 个答案:

答案 0 :(得分:0)

查看有关查询的parse.com文档。在此期间,可以使用equalTo

等方法为查询提供约束
var query = new Parse.Query(Parse.User);
query.equalTo("username", $scope.userId); 
// assuming that you're putting the username input text into $scope.userId
query.find({ success: function(array) {

           // this means the query was a success, but we're not yet certain that we found anything
           // the param to find's success is an array of PFObjects, possibly empty

           if (array.length > 0) {
               var userInfo = array[0];
               var address = userInfo.get("address");
               $scope.address = address;

               var email = userInfo.get("username");
               $scope.email = email;
               // you original code here ...

           } else {
               // the query is a success with no results,
               // which means  that no records match the criteria you gave
               alert("nothing found");
           }

       },
       error: function(object, error) {
           // The object was not retrieved successfully.
           // error is a Parse.Error with an error code and message.
                      $scope.loading = false;
       }
   });