0

My app has a service that is supposed to return an array of objects (tweets from the twitter api) that looks like:

.service('twitterService', function($http) {
    this.getTweets = function(){
        $http.get('http://example.com/?json=get_twitter_sidebar').success(
            function(data, status, headers, config) {
                if (data.status == 'ok') {
                    return data.feed;
                } else {
                    return 'Error Retrieving Feed'
                }
            });
    }

})

This service seems to work; it calls the api and returns the desired object array (I used console.log to verify). However, I can't seem to pass the returned array to my controller:

var TopPageCtrl = function($scope, $window, $http, twitterService) {
    $scope.feed = twitterService.getTweets();
};

When I use console.log($scope.feed), it returns undefined. I've tried a million approaches but nothing seems to work. What am I doing wrong?

3 Answers 3

2

return the promise

.service('twitterService', function($http) {
this.getTweets = function(){
    var p = $http.get('http://example.com/?json=get_twitter_sidebar').success(
        function(data, status, headers, config) {
            if (data.status == 'ok') {
                return data.feed;
            } else {
                return 'Error Retrieving Feed';
            }
        });
     return p;
}});


var TopPageCtrl = function($scope, $window, $http, twitterService) {
twitterService.getTweets().then(function (response) {
  $scope.feed = response;
};};
Sign up to request clarification or add additional context in comments.

Comments

1

Maybe it's returning a promise that needs to be resolved? Try something like...

var getFeed = twitterService.getTweets();
getFeed.then(function (feed) {
    $scope.feed = feed;
});

Comments

1

$scope.feed = twitterService.getTweets();

this.getTweets = function(){
            $http.get('http://example.com/?json=get_twitter_sidebar').success(
                function(data, status, headers, config) {
                    if (data.status == 'ok') {
                        return data.feed;
                    } else {
                        return 'Error Retrieving Feed'
                    }
                });
        }

Your function isn´t returning anything, that´s why you are getting "undefined".. Try this:

this.getTweets = function(){
                return $http.get('http://example.com/?json=get_twitter_sidebar').success(
                    function(data, status, headers, config) {
                        if (data.status == 'ok') {
                            return data.feed;
                        } else {
                            return 'Error Retrieving Feed'
                        }
                    });
            }

twitterService.getTweets().then(function (response){...});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.