无法在angularjs http请求中获取函数返回值

时间:2018-03-28 06:29:13

标签: javascript angularjs

我对函数返回的值是未定义的,这是一个http请求。所以我在http。

中调用http请求
bmgApp.controller('cmpUserSoftwares', function($scope, $http) {
  $scope.records = {};
  $http({
    method: 'GET',
    url: 'http://megabot/mautonew/wp-json/bmg-comp-listing/v1/company/1'
  }).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    $scope.records = response.data;
    for (var i = 0; i < $scope.records.length; i++) {
      console.log($scope.records[i]);
      angular.forEach($scope.records[i], function(value, key) {

        if (key == "maincategoryid") {
          value = getSoftwareCategory(value);
        }
        console.log(value + ' : ' + key);
      });
    }
    function getSoftwareCategory(value) {
      $http({
        method: 'GET',
        url: 'http://megabot/mautonew/wp-json/bmg-comp-listing/v1/software-category/' + value
      }).then(function successCallback(response) {
        var software_category = response.data;
        console.log(software_category);
        return software_category;
      }, function errorCallback(response) {
        console.log("Error");
      });
    }
    /*angular.forEach($scope.records, function(value, key){
         console.log( $scope.records[key].value + ' : ' + key);
         });*/
    //console.log(response.statusText);
  }, function errorCallback(response) {
    console.log("Error");
  });
});

getSoftwareCategory函数中的console.log返回值但未分配给value变量。我未定义

3 个答案:

答案 0 :(得分:0)

getSoftwareCategory正在从内部调用异步调用,它实际上并没有返回任何内容。 (调用父函数时,不返回内部then()处理函数的返回值

虽然这可能会做得更好,但作为第一步,从getSoftwareCategory返回承诺并使用then()块进行值赋值。

编辑:基于评论讨论的新代码

$http({
    method: 'GET',
    url: 'http://megabot/mautonew/wp-json/bmg-comp-listing/v1/company/1'
}).then(function successCallback(response) {
    function getSoftwareCategory(record) {
        return $http({
            method: 'GET',
            url: 'http://megabot/mautonew/wp-json/bmg-comp-listing/v1/software-category/' + record.maincategoryid
        }).then(function successCallback(response) {
            record.maincategoryid = response.data;
            return record;
        }, function errorCallback(response) {
            console.log("Error");
        });
    }
    var records = response.data;
    var promises = records.map(function (record) {
        return getSoftwareCategory(record);
    })
    return $q.all(promises).then(function (results) {
        $scope.records = results;
    });
}, function errorCallback(response) {
    console.log("Error");
});

答案 1 :(得分:0)

你是以错误的方式做到这一点我认为你不熟悉JavaScript中的异步编程。 value中的value = getSoftwareCategory(value);始终为undefined,因为您的getSoftwareCategory未返回任何值。要在代码中处理此类任务,您应首先阅读javascript中的promises。因此,在修改此

后,您的代码将如下所示
bmgApp.controller('cmpUserSoftwares', function($scope, $http) {
  $scope.records = {};
  $http({
    method: 'GET',
    url: 'http://megabot/mautonew/wp-json/bmg-comp-listing/v1/company/1'
  }).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
    $scope.records = response.data;
    for (var i = 0; i < $scope.records.length; i++) {
      console.log($scope.records[i]);
      angular.forEach($scope.records[i], function(value, key) {

        if (key == "maincategoryid") {
          getSoftwareCategory(value).then(software_category => {
            console.log(software_category)
          });
        }
        console.log(value + ' : ' + key);
      });
    }
    function getSoftwareCategory(value) {
      return $http({
        method: 'GET',
        url: 'http://megabot/mautonew/wp-json/bmg-comp-listing/v1/software-category/' + value
      }).then(function successCallback(response) {
        var software_category = response.data;
        console.log(software_category);
        return software_category;
      }, function errorCallback(response) {
        console.log("Error");
      });
    }
    /*angular.forEach($scope.records, function(value, key){
         console.log( $scope.records[key].value + ' : ' + key);
         });*/
    //console.log(response.statusText);
  }, function errorCallback(response) {
    console.log("Error");
  });
});

答案 2 :(得分:-1)

您的getSoftwareCategory函数不会返回任何值。我还要定义var software_categorythen之外声明这个,并在解析promise之后返回值。