2015-09-01 163 views
0

我的网站上的用户可以搜索movietitle,然后下拉列表将显示所有相应的标题。然后他们可以点击一个标题将该电影添加到他们的监视列表中。TypeError:无法读取undefined的属性'original_title'

当他们点击一个标题addMovie函数下面的触发器。

$scope.addMovie = function() { 

    'http://api.themoviedb.org/3/movie/206647?api_key=a8f7039633f2065942cd8a28d7cadad4&append_to_response=releases' 
    // Search for release dates using the ID. 
    var base = 'http://api.themoviedb.org/3/movie/'; 
    var movieID = $(event.currentTarget).parent().find('.movieID').text() 
    var apiKey = 'a8f7039633f2065942cd8a28d7cadad4&query=' 
    var append_to_response = '&append_to_response=releases' 
    var callback = 'JSON_CALLBACK'; // provided by angular.js 
    var url = base + movieID + '?api_key=' + apiKey + append_to_response + '&callback=' + callback; 

    $http.jsonp(url,{ cache: true}). 
     success(function (data, status, headers, config) { 

     if (status == 200) { 
      // $scope.movieListID = data.results; 
      $scope.movieListID = data; 
      console.log($scope.movieListID) 
     } else { 
      console.error('Error happened while getting the movie list.') 
     } 

     }). 
     error(function (data, status, headers, config) { 
     console.error('Error happened while getting the movie list.') 
     }); 

    createMovie.create({ 
      title: $scope.movieListID.original_title 
    }).then(init); 

    }; 

HTTP请求的工作并在控制台中我能找到对象和original_title但create方法失败。

TypeError: Cannot read property 'original_title' of undefined at Scope.$scope.addMovie

所以我猜title: $scope.movieListID.original_title没有直接保存JSON数据的正确方法是什么?

回答

1

试试这种方法。

success(function(data, status, headers, config) { 

     if (status == 200) { 
      // $scope.movieListID = data.results; 
      $scope.movieListID = data; 
      console.log($scope.movieListID); 

      createMovie.create({ title: $scope.movieListID.original_title }).then(init); 




     } else { 
      console.error('Error happened while getting the movie list.') 
     } 

    }) 

你外面http.jsonp 这是异步call.Either你怎么称呼它里面http电话或使用承诺IT连锁调用createMovie.create。 。

+0

谢谢,我认为'MovieListID'可以访问该控制器中的所有内容。 –

0

在发出$ http请求并检查一次之前,请将空对象分配给$ scope.movi​​eListID。这可能是由于起重造成的问题。我无法检查,因为没有小提琴或钢笔。

$ scope.movi​​eListID = {};

相关问题