Skip to content

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

Description

@lazd

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions