0

I am having some problems, I appear to be getting the data in the callback but can't seem to get to it, to be honest I'm not entirely sure what is going on.

When I click the "Get Movie" button, I get the following in the console:

Error: response is undefined
app</$scope.addBook@http://onaclovtech.com/apps/movies/:42:1
Wa.prototype.functionCall/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js:162:14
Mc[c]</<.compile/</</<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js:178:390
zd/this.$get</h.prototype.$eval@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js:101:134
zd/this.$get</h.prototype.$apply@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js:101:399
Mc[c]</<.compile/</<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js:178:370
Xc/c/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js:27:145
q@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js:7:357
Xc/c@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js:27:129

When I look at the network tab after the button press I get the following:

http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=[api-key]&q=The%20Way%20Of%20The%20Gun&page_limit=1&callback=angular.callbacks._0

When I look at the debugger I get a movies.json response object that looks like this:

angular.callbacks._0(
  {"total":1,"movies":[{"id":"15775","title":"The Way of the Gun","year":2000,"mpaa_rating":"R","runtime":119,"critics_consensus":"","release_dates":{"theater":"2000-09-08","dvd":"2001-06-19"},"ratings":{"critics_rating":"Rotten","critics_score":48,"audience_rating":"Upright","audience_score":71},"synopsis":"","posters":{"thumbnail":"http://content7.flixster.com/movie/11/17/79/11177993_tmb.jpg","profile":"http://content7.flixster.com/movie/11/17/79/11177993_tmb.jpg","detailed":"http://content7.flixster.com/movie/11/17/79/11177993_tmb.jpg","original":"http://content7.flixster.com/movie/11/17/79/11177993_tmb.jpg"},"abridged_cast":[{"name":"Ryan Phillippe","id":"162676004","characters":["Parker"]},{"name":"Benicio Del Toro","id":"162652510","characters":["Longbaugh"]},{"name":"James Caan","id":"162656402","characters":["Joe Sarno"]},{"name":"Juliette Lewis","id":"162654115","characters":["Robin"]},{"name":"Taye Diggs","id":"162655514","characters":["Jeffers"]}],"alternate_ids":{"imdb":"0202677"},"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies/15775.json","alternate":"http://www.rottentomatoes.com/m/way_of_the_gun/","cast":"http://api.rottentomatoes.com/api/public/v1.0/movies/15775/cast.json","reviews":"http://api.rottentomatoes.com/api/public/v1.0/movies/15775/reviews.json","similar":"http://api.rottentomatoes.com/api/public/v1.0/movies/15775/similar.json"}}],"links":{"self":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=The+Way+Of+The+Gun&page_limit=1&page=1"},"link_template":"http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"}
)

So the data all appears to be there, I am just having a hard time figuring out how to get it.

Could someone help point me in the direction of getting the data correctly?

(I do plan to move the angularjs controller to a .js file eventually, but for now it's in the .html file, sorry)

This is my original code:

Index.html

<!DOCTYPE html>
<html ng-app="myapp" manifest="covers.appcache">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.6/angular.min.js"></script>
    <script src="https://cdn.firebase.com/js/client/1.0.11/firebase.js"></script>
    <script src="https://cdn.firebase.com/libs/angularfire/0.7.1/angularfire.min.js"></script>
    <script src="rottentomatoes.js"></script>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <style type="text/css">
    <!--
      /* Move down content because we have a fixed navbar that is 50px tall */
      body {
        padding-top: 50px;
        padding-bottom: 20px;
      }
    -->
    </style>
    <!-- Optional theme -->
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
  </head>
  <body ng-controller="MyController">

    <div align="left">
       <button type="button" class="btn btn-primary btn-lg active" ng-click="addBook()">Get Movie</button>
    </div>
    <br />
    <div>
      {{data}}
      {{success}}

    <script>
      var app = angular.module("myapp", ["video"])
      .controller('MyController', ['$scope', '$video', function($scope, $video) {        
        $scope.addBook = function(e) {
          var response = $video.search('[api-key]', 'The Way Of The Gun', '1');

          response
            .success(function(data, status) {console.log('SUCCESS' + data); $scope.data = data;  $scope.status = status;})
            .error(function(data, status) {console.log('ERROR' + status); $scope.data = data; $scope.status = status;});

          // Found somewhere that said I needed a jsonp_callback function, I just tried it to see if it did anything, doesn't appear to
          function jsonp_callback(data) {console.log('JSONP' + data);  $scope.data = data; };
        }    
      }]);

      app.config(function($httpProvider) {
        //Enable cross domain calls
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
        $httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded";
      });
    </script>
  </body>
</html>

rottentomatoes.js

angular.module('video', []).factory('$video', ['$http', function($http) {
  return {
    search: function(api_key, query, page_limit) {
      var method = 'JSONP';
      var url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?";
      $http({
        method: method,
        url: url + "apikey=" + api_key +
            "&q=" + query +
            "&page_limit=" + page_limit + '&callback=JSON_CALLBACK'
      });
    }
  };
}]);

1 Answer 1

1

In the $video.search() method, you return nothing hence the error occurred.

You could just return the $http() call like this and it should work:

angular.module('video', []).factory('$video', ['$http', function($http) {
  return {
    search: function(api_key, query, page_limit) {
      var method = 'JSONP';
      var url = "http://api.rottentomatoes.com/api/public/v1.0/movies.json?";
      return $http({
        method: method,
        url: url + "apikey=" + api_key +
            "&q=" + query +
            "&page_limit=" + page_limit + '&callback=JSON_CALLBACK'
      });
    }
  };
}]);

As general tips:

  • use an unminified angular.js instead of angular.min.js while debugging, it will give you more meaningful errors.
  • no need to define the jsonp_callback function, angular will handle that for you
  • avoid naming your own service with a $ prefix i.e. $video. The $ prefix is preserved for anything that built-in in the angularjs.
Sign up to request clarification or add additional context in comments.

1 Comment

I guess the only reason I was using $ naming convention is to mimic firebase but for other services. I'll have to bug them to find out why they used $firebase.

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.