user3340037
user3340037

Reputation: 731

AngularJS Promise resolved incorrectly

I have a router that's waiting for these properties to be resolved

$stateProvider.state('friends', {
            url: '/friends/{id}',
            templateUrl: '/friends.html',
            controller: 'friendsCtrl',
            resolve: {
                friend: ['$stateParams', 'friends', function($stateParams, friends){
                    return friends.get($stateParams.id);
                }],
                notes: ['$stateParams', 'friends', function($stateParams, friends){
                    return friends.getNotes($stateParams.id);
                }]
            }
        });

This is friends.get and friends.getNotes.

friends.get = function(id){
        return $http.get('/friends/' + id).then(function(res){
            return res.data;
        });
    };

    friends.getNotes = function(id){
        return $http.get('/friends/' + id + '/notes').then(function(res){
            console.log(res.data);
            return res.data;
        });
    }; etc... (10 more methods)

friends is an object with approximately 12 different methods, getNotes and get just being two of them. I've checked the console log for getNotes and get. get returns a friend object (as I want), and getNotes returns an array of notes (also as I wanted).

This issue comes inside the controller. friend gets resolved to a friend object as I want. HOWEVER, notes gets resolved as the entire friends object (it's not an array, it happens to literally be the friends object with methods like get and getNotes).

This is how I inject friend and notes into the controller

app.controller('friendsCtrl', ['$scope', 'friend', 'notes', 'friends',
    function($scope, friend, friends, notes){
        $scope.doComment = false;
        $scope.friend = friend;
        console.log(friend);
        $scope.notes = notes;
        console.log(notes);

Logging friend gives me a friend object. Logging notes doesn't give me the array of notes as expected, rather I get the ENTIRE friends object. So instead of [note1, note2, note3] I get Object with methods getNotes, get, etc. Any ideas why I'm getting the friends object rather than the notes array in notes?

Upvotes: 1

Views: 45

Answers (1)

dfsq
dfsq

Reputation: 193261

The order of injected services is incorrect. Should be

function($scope, friend, notes, friends)

according to ['$scope', 'friend', 'notes', 'friends', function() {...}.

Upvotes: 2

Related Questions