Javascript 工厂法不适用于';t return-TypeError:无法读取属性';然后';未定义的
我被这个问题缠住了。我已经了解到我的工厂方法应该返回,但是我在代码中尝试了许多不同的位置,用于Javascript 工厂法不适用于';t return-TypeError:无法读取属性';然后';未定义的,javascript,angularjs,angularjs-scope,angularjs-service,Javascript,Angularjs,Angularjs Scope,Angularjs Service,我被这个问题缠住了。我已经了解到我的工厂方法应该返回,但是我在代码中尝试了许多不同的位置,用于返回,但都没有效果 我呼叫服务的控制器: $scope.updateChosenSet = function(){ var chosenMeds = $scope.medications.chosenMedications; if(chosenMeds.length){ var n = ['provigil', 'improvest', '
返回
,但都没有效果
我呼叫服务的控制器:
$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
s迭代器函数返回承诺(尽管它没有任何用处),它只是从该函数返回值,而不是从外部函数返回。你所需要做的就是回报一个承诺,这个承诺将解决所有潜在的承诺。因此,使用并返回return$q.all(fff.map(_makeCall))代码>来自您的服务方法。q、 只有当所有基本承诺都已解决时,所有承诺才会解决,如果其中一个承诺被拒绝,则整个承诺集都将被拒绝
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
});
您没有从callToAPI
函数返回任何内容。从forEach迭代器函数makeCall
返回时,不会从父函数返回。@PSL当前代码不会显示它-但我已尝试返回。也就是说,我试图返回callToAPI
这是一种方法吗?你不会得到然后
作为魔术:/,函数callToApI
需要返回一个承诺,尝试返回$q.all(fff.map(makeCall))
@PSL谢谢,现在我知道我不太了解$q
,我会花一些时间来完全理解它。请将您的建议移至答案,以便我可以接受!是(有限)Q库的角度版本$q.all
将确保仅在解析所有传递的承诺数组时解析。如果您的一个呼叫失败,它将转到catch block。你试过了吗?