Search code examples
angularjsangular-resource

Trying to get a certain user by username into a variable


Im trying to add a user profile page to a webpage so i need to get the details for a user by username. Im using $stateparams which is getting the username fine.

The im using this function which is throwing an error

      $scope.ourUser = Users.get({
            username: $stateParams.username
        });

Error

[$resource:badcfg] Error in resource configuration for action `get`. Expected response to contain an object but got an array

Controller code seems alright

angular.module('savings').controller('SavingsController', ['$scope',  '$http', '$timeout', '$stateParams', '$location', '$window','$state' , 'Authentication', 'Savings', 'FileUploader', 'Posts', 'Users',
function($scope,  $http, $timeout, $stateParams,  $location, $window, $state, Authentication, Savings, FileUploader, Posts, Users) {

Ive edited the question to simplify it as the Posts call wasnt where the problem is.

Ive added this to the services

userByUsername: {
    method: 'GET',
    url: '/api/users/:userName',
    isArray: true
  }

Solution

  • When you declare a resource you have a property isArray available like this :

    'search':   {method:'GET', isArray:true,....}
    

    Set it to true when your service return a list or it will fail with that message.

    This means that the variable ourUser will be an array of [expected] one user.