工厂方法没有't return-TypeError:无法读取属性'那么'的未定义
Factory method doesn't return - TypeError: Cannot read property 'then' of undefined
我遇到了这个问题。我知道我的工厂方法应该返回,但我在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
});
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- 无法读取属性'材料'未定义的Three.js
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- Soundcloud Javascript SDK 3.0-回调无法读取属性'connectCallback
- 未捕获的类型错误:无法读取属性'状态'在react中为null
- JavaScript承诺-无法读取属性'那么'的未定义
- Javascript Float32数组抛出无法读取属性'0'即使数组定义良好,也为null
- 未捕获的类型错误:无法读取属性'中止'的未定义
- 角度推入数组给出:TypeError:无法读取属性'推'的未定义
- http/rxjs catch回调中的Angular 2重定向导致TypeError:无法读取属性'订阅'
- Javascript未捕获类型错误:无法读取属性'0'的未定义
- jQuery控制台错误:无法读取属性'top'的未定义
- TypeError:无法读取属性'findAll'的未定义