Thursday, 11 May 2017

$HTTP GET and returned Undefined Object

I've setup a controller and service to grab some JSON from my own NodeJS API/MongoDB endpoint.

In my browser console I see a returned object and 5 items and when I query my api I see the same json in the browser, so I know it's working on the server side.

I'm confused though about why when attempting to NG-Repeat through this I get nothing on page and when I console log out the returned data, it comes back undefined.

I'm new to the HTTP module and I'm trying to refactor out DB calls to a service instead of using it in controller.

--- Controller code ---

vm.getDepartments = function() {
        vm.departments = DbService.getAllDepartments();
        console.log(vm.departments);

    }();

--- Service Code (using $http) ---

function getAllDepartments() {
        $http.get('/api/departments').then(function(err, response) {
            if (err) {
                console.log(err);
            }
            return response;

        });
    };

--- html page ---

<tbody>
                    <tr ng-repeat='dept in vm.departments'>
                        <td></td>
                        <td></td>
                        <td>
                            <button class='btn btn-warning'><span class='glyphicon glyphicon-edit'></span></button>
                            <button class='btn btn-danger' ng-click='vm.deleteDepartment(dept);'><span class='glyphicon glyphicon-ban-circle'></span></button>
                        </td>
                    </tr>
                </tbody>



via mike varela

No comments:

Post a Comment