【问题标题】:Returning REST Data To Another Function将 REST 数据返回到另一个函数
【发布时间】:2016-05-27 00:12:33
【问题描述】:

所以我将 AngularJS 合并到我正在创建的 SharePoint 页面中。我有一个函数可以通过 REST 函数从 SharePoint 中的列表中收集信息。它应该做的是用它从 REST 调用中检索到的数据填充一个表。它做得很好,但是,其中一个字段是“人员或组”字段,而不是返回人的姓名,而是返回他们的 ID,这很好,但它要求我在另一个列表上执行另一个 REST 调用。我已经成功地这样做了,并根据用户 ID 过滤掉了结果。我所做的是将用户 ID 传递给一个函数,该函数将 REST 函数调用到包含用户名的列表,然后编写一个 if 语句,基本上说如果传递的 ID 等于给定的元素 ID,则显示用户名。我遇到的问题是,当我检索到正确的数据时,我无法返回数据,我相信原因是因为我试图从嵌套函数返回数据。我有一个函数,在该函数中有一个 $http.success 函数。我试图检索的数据位于 $http.success 函数中。抱歉,如果这个问题很难理解,但希望看看下面的代码将有助于解决问题。

附:当我从“displayProjDetails”函数提醒代码时,我收到一条未定义的消息,但是当我从“getUser”函​​数提醒它时,它会正确显示数据。

提前感谢您的帮助。

角码:

// Function to display Project Details Data
        $scope.displayProjDetails = function() {

            $http({type: "GET", url:"http://mysiteurl/_api/web/lists/getbytitle('Project%20Details')/items?$top=5000", headers: { "ACCEPT": "application/json;odata=verbose"}})

            .success(function(data) {

                $scope.results = data.d.results;

                $scope.details = [];

                var full_funded;

                for(i=0; i < data.d.results.length; i++) {

                    if(data.d.results[i].gibi == $scope.selectedProject.id) {

                        alert($scope.getUser(data.d.results[i].Project_x0020_POCId)); // CALL TO FUNCTION I AM WORKING ON (LOCATED AT THE BOTTOM OF THE PAGE)

                        if(data.d.results[i].Fully_x0020_Funded == true) {
                            full_funded = "Yes";
                        } else {
                            full_funded = "No"
                        }

                        $scope.details.push({id: data.d.results[i].gibi, poc: data.d.results[i].Project_x0020_POCId, code_poc: data.d.results[i].Code_x0020_312_x0020_POCId, 
                                           perc_complete: data.d.results[i].OData__x0025__x0020_Complete, funded: full_funded, pop_from: data.d.results[i].PoP_x0020_From,
                                           pop_to: data.d.results[i].PoP_x0020_To});
                    }
                };

            }); 
        }

// Function to retrieve the name of the Point of Contact (Currently working on this...)
        $scope.getUser = function(value) {

            $http({type: "GET", url: "http://mysiteurl/_api/web/siteusers?$top=5000", headers: { "ACCEPT": "application/json;odata=verbose"}})

            .success(function(data) {

                $scope.results = data.d.results;

                $scope.user_attributes = [];

                for(i=0; i < data.d.results.length; i++) {  
                    if(data.d.results[i].Id == value) { 
                        return data.d.results[i].Title;
                    }
                };  
            });

        }

HTML 代码

<div class="col-lg-9" id="project_details_table"> 
           <h3>Project Details</h3> 
           <table class="table table-striped"> 
              <thead> 
                 <tr> 
                    <th>Project ID</th> 
                    <th>PoC</th> 
                    <th>Code 2532 PoC</th> 
                    <th>% Complete</th> 
                    <th>Fully Funded</th> 
                    <th>PoP From</th> 
                    <th>PoP To</th> 
                 </tr> 
              </thead> 
              <tbody>
                 <tr data-ng-repeat="detail in details">
                    <td data-ng-bind="detail.id"></td>
                    <td data-ng-bind="detail.poc"></td> <!--TRYING TO REPLACE THIS WITH THE USER'S NAME INSTEAD OF THE USER'S ID-->
                    <td data-ng-bind="detail.code_poc"></td>
                    <td data-ng-bind="detail.perc_complete"></td>
                    <td data-ng-bind="detail.funded"></td>
                    <td data-ng-bind="detail.pop_from | date:'yyyy/MM/dd'"></td>
                    <td data-ng-bind="detail.pop_to | date:'yyyy/MM/dd'"></td>
                 </tr> 
              </tbody> 
           </table> 
        </div>

更新: 对我的代码进行建议的更改后,我的数据显示如下...

如您所见,我正在尝试用正确的名称替换“[object Object]”。在这里,它只是向表中添加新行,而不是将名称放在正确的位置。这是我更新的代码...

$scope.getUser = function(value) {

            return $q(function(resolve, reject){

                $http({type: "GET", url: "http:mysiteurl/_api/web/siteusers?$top=5000", headers: { "ACCEPT": "application/json;odata=verbose"}})

                .success(function(data) {

                    $scope.results = data.d.results;

                        for(i=0; i < data.d.results.length; i++) {  
                            if(data.d.results[i].Id == value) { 
                                resolve(data.d.results[i].Title);
                            }
                        }; 

                });
            });
        }

// Function to display Project Details Data
        $scope.displayProjDetails = function() {

            $http({type: "GET", url:"http:mysiteurl/_api/web/lists/getbytitle('Project%20Details')/items?$top=5000", headers: { "ACCEPT": "application/json;odata=verbose"}})

            .success(function(data) {

                $scope.results = data.d.results;

                $scope.details = [];

                $scope.name = [];

                var full_funded;

                for(i=0; i < data.d.results.length; i++) {

                    if(data.d.results[i].gibi == $scope.selectedProject.id) {

                        $scope.name = $scope.getUser(data.d.results[i].Project_x0020_POCId).then(function(user){$scope.details.push({poc: user});});

                        if(data.d.results[i].Fully_x0020_Funded == true) {
                            full_funded = "Yes";
                        } else {
                            full_funded = "No"
                        }

                        $scope.details.push({id: data.d.results[i].gibi, poc: $scope.name, code_poc: data.d.results[i].Code_x0020_312_x0020_POCId, 
                                           perc_complete: data.d.results[i].OData__x0025__x0020_Complete, funded: full_funded, pop_from: data.d.results[i].PoP_x0020_From,
                                           pop_to: data.d.results[i].PoP_x0020_To});
                    }
                };

            }); 
        }

【问题讨论】:

    标签: angularjs function rest sharepoint return


    【解决方案1】:

    问题是你正在处理异步调用,所以你不能返回数据,因为警报不等待承诺响应,所以解决问题的一种方法是从 getUser 返回承诺:

    return $http ...
    

    然后在警报中:

    alert($scope.getUser(data.d.results[i].Project_x0020_POCId).then(function(name){return name;}));
    

    【讨论】:

      【解决方案2】:
      $scope.getUser = function(value) {
           return $q(function(resolve, reject){
                  $http({type: "GET", url: "http://mysiteurl/_api/web/siteusers$top=5000",
                         headers: { "ACCEPT": "application/json;odata=verbose"}})
      
                  .success(function(data) {
      
                      $scope.results = data.d.results;
      
                      $scope.user_attributes = [];
      
                      for(i=0; i < data.d.results.length; i++) {  
                          if(data.d.results[i].Id == value) { 
                              resolve(data.d.results[i].Title);
                          }
                      };  
                  });
              }
      
      
      
      
      $scope.getUser(data.d.results[i].Project_x0020_POCId)
      .then(function(user){
         alert(user)
       });
      

      【讨论】:

      • 这与我想要完成的工作很接近,但是,我没有提醒我需要返回它们的值,所以我可以将它们传递给 $scope.details 变量来代替“poc”多变的。当我将“警报”更改为“返回”时,当我去提醒该功能时,我会收到 [Object object]。
      • 您可以将其传递给 scope.details 从它所在的位置。将其余代码添加到警报所在的位置。
      • 所以你是说将代码从“if 语句到 $scope.details”放在“then”函数内?抱歉,这对我来说没有意义,因为没有办法识别“data.d.results[i].whatever”,因为它是在“then”函数之外定义的。如果我遇到困难,我深表歉意,我对 Angular 还是新手,并试图理解你想说的话。非常感谢您的所有帮助。
      • 只需将 data.d.results[i] 保存到一个变量中,然后从 then 函数中访问它。它是一个闭包,因此您仍然可以访问在父作用域中声明的变量。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 2010-09-15
      • 1970-01-01
      • 2023-01-28
      相关资源
      最近更新 更多