Ali_bean
Ali_bean

Reputation: 341

Angular Service "Unable to get property 'then' of undefined or null reference"

Hello I can't figure out how to return the correct function / part of the service since it's paignating because of the Yammer API.

I've read documentation and tried returning the whole function but I've spent so long on it I'm starting to despair, please help me.

The return is in the wrong place I think - I get the error:

"Unable to get property 'then' of undefined or null reference"

My Controller

app.controller("mainController", function($scope, $http, yammerREST) {

    $scope.getYammerPosts = function () {
                yammerREST.getYammerData($scope.yammerURL).then(function(data) {
                        $scope.results = data.results;
                });
    };
});

My service

app.service("yammerREST", function($http) {
    this.getYammerData = function(url) {

        var groupID = url.split("feedId=")[1];
        console.log(groupID);
        var baseURL = "https://www.yammer.com/api/v1/messages/in_group/" + groupID + ".json?threaded=true";
        var url = baseURL;
        var results = [];

        getPosts();

        function getPosts () {
        return $http({
                url: url,
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" }
             }).then(function(response) {
                    results = results.concat(response.data.messages);
                    console.log(results);

                    if (response.data.meta.older_available == true) {
                            url = baseURL + "&older_than=" + results[results.length-1].id;
                            getPosts();
                    };

                    return {
                        yammerListName: response.data.meta.feed_name,
                        results: results,
                    };
            }).catch(function(e){
                console.log("Error: ", e);
            });
        };
    };
});

Upvotes: 0

Views: 2800

Answers (1)

georgeawg
georgeawg

Reputation: 48968

“Unable to get property 'then' of undefined or null reference”

That error message usually means the function failed to return the promise:

app.service("yammerREST", function($http) {
    this.getYammerData = function(url) {

        var groupID = url.split("feedId=")[1];
        console.log(groupID);
        var baseURL = "https://www.yammer.com/api/v1/messages/in_group/" + groupID + ".json?threaded=true";
        var startURL = baseURL;
        var results = [];

        //vvvv RETURN promise
        return getPosts(baseURL,startURL);

        function getPosts (baseURL, url) {
            //vvvv RETURN promise
            return $http({
                url: url,
                method: "GET",
                headers: { "Accept": "application/json; odata=verbose" }
            }).then(function(response) {
                    results = results.concat(response.data.messages);
                    console.log(results);

                    if (response.data.meta.older_available == true) {
                        var nextURL = baseURL + "&older_than=" + results[results.length-1].id;
                        //vvvv RETURN promise
                        return getPosts(baseURL,nextURL);
                    };

                    return {
                        yammerListName: response.data.meta.feed_name,
                        results: results,
                    };
            }).catch(function(e){
                console.log("Error: ", e);
                //IMPORTANT to re-throw error
                throw e;
            });
        };
    };
});

The getPosts function is being called recursively. To do effective recursion, f(x) has to call f(x+1) until some end condition is met.

Also in .catch blocks it is important to re-throw errors in order to skip subsequent .then blocks in the promise chain.

Upvotes: 2

Related Questions