2

I am working on an apps to read JSON file.

Currently I am able to retrieve and read the JSON file but the problem is when to choose data in JSON.

Error "Cannot read property 'length' of undefined"

Below is my code.

Services.js

.factory("mainData", function($http, $log, $q) {
    var chartData;

    return {

        all: function() {
            var d = $q.defer();
            $http({ method: 'GET',
                url: 'http://localhost:8080/aZolla/getGlazCharts.do'})
                    .success(function (data, status, header, config) {
                         d.resolve(data);
                        })
                    .error(function (data, status, header, config) {
                        $log.warn(data, status, header, config);
            });

            chartData = d.promise;

            return d.promise;     
        },

       get: function(chartID) {
        for (var i = 0; i < chartData.length; i++) {
        if (chartData[i].id === parseInt(chartID)) {
          return chartData[i];
        }
      }
      return null;
    }
 };
});

Controller.js

.controller("ChartsListsCtrl", function($scope, mainData) {

    mainData.all().then(function(data){
    $scope.chartLists = data;
    }            
   )

.controller('ChartsCtrl', function($scope, $stateParams, mainData) {

  $scope.chart = mainData.get($stateParams.chartId);

})
2
  • You load both controllers simultaniously? This looks like an asyncronic problem. Commented May 27, 2016 at 4:39
  • Check whether the chartData contains expected data during the execution of get function. Commented May 27, 2016 at 5:20

2 Answers 2

1

Try This

.controller("ChartsListsCtrl", function($scope, mainData) {

    mainData.all().then(function(data){
    $scope.chartLists = data;
    }            
   )

   .controller('ChartsCtrl', function($scope, $stateParams, mainData) {
      mainData.all().then(function(data){
       $scope.chart = mainData.get($stateParams.chartId);
      })
   })

You need to load all before get call. In your first controller all call being happened. But it does not mean in your second controller it will be available, because your second controller may be initialized same time with your first controller and till then may be your all call is not getting finished.

Sign up to request clarification or add additional context in comments.

Comments

0

You should pass chartData to your get function. Try this:

get: function(chartID, chartData) {
  for (var i = 0; i < chartData.length; i++) {
    if (chartData[i].id === parseInt(chartID)) {
      return chartData[i];
  }
}

In controller:

.controller('ChartsCtrl', function($scope, $stateParams, mainData) {
  mainData.all().then(function(data){
    $scope.chart = mainData.get($stateParams.chartId, data);
  });
})

1 Comment

Glad to help you. And be careful about working with $q service. You shouldn't use $q service in $http request. Because http request returns promise by itself. See my question here: stackoverflow.com/questions/36631601/…

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.