3

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.

Error

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.

fruitsFactory.js

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);    
            });     
       }
   };    
});

controller.js

app.controller('fruitsController',['$scope','fruitsData', function($scope, fruitsData){
    fruitsData.getFruitsData(function(fruits){
        $scope.fruits = fruits;
    });
}]);
6
  • succes typo? It's success Commented Apr 28, 2014 at 9:51
  • it's all about async: stackoverflow.com/questions/16286605/… Commented Apr 28, 2014 at 9:51
  • The error in this particular case is unrelated to async calls Commented Apr 28, 2014 at 10:01
  • 1
    It's not about async...it's all about a typo just like what Dieter Goetelen said. You're trying to invoke a non existant method called succes(arg) instead of calling success(arg). Commented Apr 28, 2014 at 10:03
  • Yes It is about the typo. So I am able to retrieve data from the JSON file, but the question now is how can I retrieve the data from a database? I got a MySQL database and I have a PHP file to fetch the data and to encode it to JSON. But what do I do with it after that? Commented Apr 28, 2014 at 10:51

1 Answer 1

2

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;
  });
}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.