So I have two functions within an angular service, a getRatings function function that calls a mongoDb database and returns a JSON object of ratings, and a getAverageRatings service that calls the getRatings function which is then supposed to return an average of the ratings.
The problem is is that I'm getting undefined when I return the calculated average, even when I see that I'm getting the correct calculated average from my console.logs. One of my co-workers said that I'm not using a promise correctly and that's what I need to return and resolve to get the correct value, but after trying a few things I cannot figure it out.
 getRatings: function (contentId, callback) {
        console.log("in getRatings");
        var deferred = $q.defer();
        return $http.get(CONTENT_API + '?contentId=' + contentId)
            .then(function (response) {
                if(typeof response.data === 'object'){
                    console.log("Promise in contentRatingService successful, returning promise");
                    deferred.resolve(response.data);
                    return response.data;
                }
                else {
                    return $q.reject(response.data);
                }
        }, function(response){
                // something went wrong
                return $q.reject(response.data);
            });
    },
and
getAverageRating: function (contentId) {
        this.getRatings(contentId).then(function(data){
            var allRatings = data;
            console.log("getAverageRating data: ");
            console.log(allRatings);
            var ratingTotal = 0;
            for (var i =0; i<allRatings.length; i++) {
                console.log("rating " + i + " is " + allRatings[i].rating);
                ratingTotal = ratingTotal + allRatings[i].rating;
            }
            var average = ratingTotal/allRatings.length;
            console.log("Length of allRatings: "+ allRatings.length);
            average = Math.round(average);
            console.log("The average is: " + average);
            //deferred.resolve();
            return average;
        }, function(error) {
            //promise rejected
            console.log(error);
        });
    },
I'm calling this code from an angular 1.5 component controller like this:
$scope.averageRating = contentRatingService.getAverageRating("1232412");
Here is a screenshot of my logs to show that I'm calculating the average correctly, but getting undefined when assigning it to an actual variable:
Any help would be greatly appreciated.
