Busy building an video website and i'm currently rewritting the code but i can't get past this issue. So i got a controller.js where i want to do all the request and in my main.js i want to receive the data and parse is.
So my controller.js looks like:
'use strict';
angular.module('Movie.services', []).service('Movie', ['$q', '$http', function($q, $http) {
var API_URL = 'http://api.themoviedb.org/3/person/';
var API_KEY = '?api_key=xxx';
this.findCast = function( id ) {
var cast = [];
$http.get(API_URL, {
params: {
query: id,
api_key: API_KEY
}
}).then(function(result) {
angular.forEach(result.data, function(item) {
cast.push(item);
});
});
}
}]);
JSON: http://docs.themoviedb.apiary.io/reference/people/personid/get And my main.js looks like:
app.controller("Cast", function($scope, $http, $routeParams, Page, Movie) {
$scope.getCast = [];
var person_id = $routeParams.person_id;
Movie.findCast(person_id).then(function(result){
$scope.getCast = result ;
});
});
My question is how can i receive the data from my controller.js in main.js so i can parse it. What i now get is an error
TypeError: Cannot read property 'then' of undefined
I'm not sure if this is the right infrastructure to build my app because in the controller.js i want to make multiple calls for example i want to get the cast, movies, tv series and so on.
Controller.js will look like this (don't know if its the right way to do it)
this.findCast = function( id ) { }
this.getMovies = function( id ) { }
this.getSeries = function( id ) { }
this.getPopulair = function( id ) { }
The problem is you are not returning any Promise from your service, on which you can call then. Do something like this.
'use strict';
angular.module('Movie.services', []).service('Movie', ['$q', '$http', function($q, $http) {
var movieService = {
findCast : function( id ) {
var API_URL = 'http://api.themoviedb.org/3/person/';
var API_KEY = '?api_key=xxx';
var cast = [];
var promise = $http.get(API_URL, {
params: {
query: id,
api_key: API_KEY
}
}).then(function(result) {
angular.forEach(result.data, function(item) {
cast.push(item);
});
return cast;
});
return promise;
},
// getMovies : function(){}
// similarly write other functions.
}
return movieService;
}]);