I am trying to load data from my JSON file through a $http call in my Factory and every time I run the code I get the same error. How can I fix this.
TypeError: undefined is not a function
at Object.getFruitsData (http://localhost/test/JSON/js/controllers.js:12:18)
at new <anonymous> (http://localhost/test/JSON/js/controllers.js:3:16)
at invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:3869:17)
at Object.instantiate (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:3880:23)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:7134:28
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:6538:34
at forEach (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:330:20)
at nodeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:6525:11)
at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:5986:15)
at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:5989:13)
the code is which I am using is down below.
app.factory('fruitsData', function($http, $log){
return{
getFruitsData: function(succescb){
$http({method:'GET', url:'json/testList.json'})
.succes(function(data){
succescb(data);
})
.error(function(data){
$log.warn(data);
});
}
};
});
app.controller('fruitsController',['$scope','fruitsData', function($scope, fruitsData){
fruitsData.getFruitsData(function(fruits){
$scope.fruits = fruits;
});
}]);
you are never returning the data and you are creating a new function to pass into the factory function (mistake?)
try this:
app.factory('fruitsData', ['$http', '$log', function($http, $log) {
return {
getFruitsData: function() {
.success(function(fruitData) {
return fruitData;
})
.error(function(errorMessage) {
//log the error message
});
}
}
}]);
app.controller('fruitController', ['$scope', 'fruitsData', function($scope, fruitsData) {
fruitsData.getFruitsData().then(function(data) {
$scope.fruits = data;
});
}]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With