Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factory.getRelatedTags produces JS error when no items found #169

Open
lazd opened this issue Feb 28, 2014 · 0 comments
Open

factory.getRelatedTags produces JS error when no items found #169

lazd opened this issue Feb 28, 2014 · 0 comments

Comments

@lazd
Copy link

lazd commented Feb 28, 2014

At the following URL: http://readup.co/#/pinky

TypeError: Cannot read property 'length' of undefined
    at http://readup.co/build/bundle.js:682:40
    at http://readup.co/lib/bower_components/angular/angular.js:9137:11
    at wrappedCallback (http://readup.co/lib/bower_components/angular/angular.js:6995:59)
    at http://readup.co/lib/bower_components/angular/angular.js:7032:26
    at Object.Scope.$eval (http://readup.co/lib/bower_components/angular/angular.js:8218:28)
    at Object.Scope.$digest (http://readup.co/lib/bower_components/angular/angular.js:8077:25)
    at Object.Scope.$apply (http://readup.co/lib/bower_components/angular/angular.js:8304:24)
    at done (http://readup.co/lib/bower_components/angular/angular.js:9357:20)
    at completeRequest (http://readup.co/lib/bower_components/angular/angular.js:9520:7)
    at XMLHttpRequest.xhr.onreadystatechange (http://readup.co/lib/bower_components/angular/angular.js:9490:11) angular.js:5930

It's happening in this method:

  factory.getRelatedTags = function(tagName){
    var deferred = $q.defer();
    var requestURL = '/_/tags/' + tagName + '/items/1';
    $http.get(requestURL).success(function(data){
      var results = {};
      for(var i = 0; i < data.length; i++){
        for(var j = 0; j < data[i].tags.length; j++){
          if(data[i].tags[j].name !== tagName){
            results[data[i].tags[j].name] = data[i].tags[j].name;
          }
        }
      }
      deferred.resolve(results);
    });
    return deferred.promise;
  };

At the time of execution, data was:

[{"title":"No items found."}]

Since the object doesn't have a tags property, accessing tags.length fails and the promise is rejected. Check for the presence of tags before attempting to iterate over them.

@svnh svnh mentioned this issue May 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant