1

As you can see this is my first time attempting this and I appear to be doing it incorrectly. I just want to take some code, consisting of promises and http requests, and put it in a service before the controller uses it. My goal is to simply clean up the controller so it doesn't contain all of that code.

After logging it in the last step of the controller the object appears as undefined. Also, all the requests are being made successfully. So, it's jumping through all the hoops fine so I'm guessing it must not be returning any value in the service and nothing gets passed on to the subsequent function in the controller. How can I return the 'people' array in the service after the promises have been fulfilled?

var myApp = angular.module('myApp', []);

myApp.controller('AppCtrl', function ($scope, $http, dataService) {

    var getPeople = function () {
        return $http.get('/getpeople');
    };

    getPeople().then(function (response) {
            dataService.compilePeople(response)
        })
        .then(function (people) {
            console.log(people);
            $scope.people = people;
        });

});

myApp.service('dataService', function ($q, $http) {

    this.compilePeople = function (response) {
        var people = [];
        names = response.data;
        grandPromiseArray = [];

        names.forEach(function (index) {
            var name = index,
                count = $q.defer(),
                skills = [],
                urls = '/getskillsbyname/' + name,
                urlc = '/getcountbyname/' + name;

            grandPromiseArray.push(
                $q.all([$http.get(urls), $http.get(urlc)])
                    .then(function (response) {
                        people.push({
                            name: name,
                            skills: response[0].data,
                            count: response[1].data
                        });

                    })
            );

        });
        return $q.all(grandPromiseArray).then(function () {
            return people
        });
    }
});
2
  • i thing you process all your logic from server not from client , when you call many ajax services always not get the best result you want by the asyncronus call Commented Feb 26, 2016 at 20:22
  • Which object is undefined? compilePeople returns a promise that cannot be undefined. Commented Feb 26, 2016 at 20:42

1 Answer 1

2

You need to return the promise from compilePeople() in order for the people to be passed into the next .then() handler. so close ;)

getPeople()
    .then(function (response) {
        //You were missing this return
        return dataService.compilePeople(response)
    })
    .then(function (people) {
        console.log(people);
        $scope.people = people;
    });
Sign up to request clarification or add additional context in comments.

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.