I am trying to adapt a NodeJs/AngularJs tutorial http://sahatyalkabov.com/create-a-tv-show-tracker-using-angularjs-nodejs-and-mongodb/ to using Postgres and the Sequelize ORM. The ORM has changed as it now uses Bluebird promises.
I'm hitting my database to return a number of TV shows and in return I get a list of shows and their episodes. This is the html before I used promises:
<div class="col-xs-4 col-md-3" ng-repeat="show in shows | filter:query | orderBy:'rating':true">
<a href="/shows/{{show.id}}"><img class="img-rounded" ng-src="{{show.poster}}" width="100%"></a>
<div class="text-center">
<a href="/shows/{{show.id}}">{{show.name}}</a>
<p class="text-muted">Episodes: {{show.episodes.length}}</p>
</div>
</div>
So I use the $resource service and a ng-repeat loop, however since I changed to promises I don't seem to be able to access the episodes, but in my console I see the $scope.shows is equal to:
[$promise: Object, $resolved: false]
0: Resource
$$hashKey: 57
Episodes: Array[68]
airs_day_of_week: "Sunday"
airs_time: "9:00 PM"
createdAt: "2014-10-14T10:29:27.287Z"
first_aired: "2008-01-20T00:00:00.000Z"
genre: Array[4]
id: 81189
name: "Breaking Bad"
network: "AMC"
overview: "Walter White, a struggling high school chemistry teacher, is diagnosed with advanced lung cancer. He turns to a life of crime, producing and selling methamphetamine accompanied by a former student, Jesse Pinkman, with the aim of securing his family's financial future before he dies."
poster: "posters/81189-22.jpg"
rating: "9.30"
rating_count: 645
status: "Ended"
updatedAt: "2014-10-14T10:29:27.287Z"
__proto__: Resource
$promise: Object
$resolved: true
length: 1
__proto__: Array[0]
How can I access the episodes.length now? show.name works but show.episodes.length returns undefined.
My resource invocation looks like this:
in the controller:
$scope.shows = Show.query();
in the services:
angular.module('MyApp')
.factory('Show', ['$resource',
function ($resource) {
return $resource('/api/shows/:id');
}]);