工厂方法没有't return-TypeError:无法读取属性'那么'的未定义

Factory method doesn't return - TypeError: Cannot read property 'then' of undefined

本文关键字:读取 属性 未定义 那么 return-TypeError 方法 工厂      更新时间:2023-09-26

我遇到了这个问题。我知道我的工厂方法应该返回,但我在return的代码中尝试了许多不同的位置,但都没有成功。

我呼叫服务的控制器:

    $scope.updateChosenSet = function(){
        var chosenMeds = $scope.medications.chosenMedications;
        if(chosenMeds.length){
            var n = ['provigil', 'improvest', 'provenge'];
            // in console, Angular is complaining about the below line (just before the dot)
            medicationByNameFactory.callToAPI().then(
                    function(data){
                        console.log("Works!");  // this never fires in the console!!
                    }
                );
        }
    };

以及我的服务:

angular.module('hsToolkit.services')
        .factory('medicationByNameFactory', medicationByName);

medicationByName.$inject = ['$http'];
function medicationByName($http){

    // first returning callable properties as suggested here: https://github.com/johnpapa/angularjs-styleguide
    // tried the conventional way, but it's the same
    return {
        callToAPI: callToAPI
    };

    function callToAPI(){
        // this array will be supplied as an argument from controller when this starts to work
        var fff = ['provigil', 'improvest', 'provenge'];
        angular.forEach(fff, makeCall);
        function makeCall(item){
            return $http({
                    method: 'GET',
                    url: 'path/to/api/?name='+item,
                    headers: {
                        'Content-type': 'application/json'
                    }
                })
                    .then(
                        function(response){
                            // this DOES output to console!
                            console.log(response.data.drugGroup);
                            // I'm getting error with or w/o this!
                            return response.data.drugGroup;
                        }
                    );
        } // closing: makeCall
    }; // closing: callToAPI
}; // closing: medicationByName

您的问题是,您没有从工厂的callToApI方法返回任何内容,即使您从forEach的迭代器函数返回promise(尽管没有任何用处),它也只是该函数的返回值,而不会从外部函数返回。你所需要做的就是回报一个能解决所有潜在承诺的承诺。因此,请使用$q.all并从您的服务方法返回return $q.all(fff.map(_makeCall));。q.all只有在所有基本承诺都已解决的情况下才会解决,如果其中一个承诺被拒绝,整个承诺都将被拒绝。

medicationByName.$inject = ['$http', '$q'];
function medicationByName($http){
   return {
        callToAPI: callToAPI
    };

    function callToAPI(){
        var fff = ['provigil', 'improvest', 'provenge'];
        return $q.all(fff.map(_makeCall));
   }; 
    function _makeCall(item){
            return $http({
                    method: 'GET',
                    url: 'path/to/api/?name='+item,
                    headers: {
                        'Content-type': 'application/json'
                    }
                }).then(function(response){
                    // this DOES output to console!
                     console.log(response.data.drugGroup);
                     // I'm getting error with or w/o this!
                     return response.data.drugGroup;
                });
        }
   };

并且在您的控制器中:-

medicationByNameFactory.callToAPI().then(function(data){
    console.log("Works!");  // this never fires in the console!!
}).catch(function(){
   //Atleast one of the call failed
});