3

I have the following resource definition:

myservices.factory('User', ['$resource',
    function($resource){
        return $resource('/user/:screenName', {}, {
            whoami: {
                url: '/user',
                method: 'GET',
                isArray: false,
                params: {
                    op: 'whoami'
                }
            },
            query: {
                url: '/user',
                method: 'GET',
                isArray: true
            },
            get: {
                method: 'GET',
                isArray: false
            },
            update: {
                method:'GET',
                params: {
                    op: 'update'
                },
                isArray: false,
            },
            delete: {
                method:'GET',
                params: {
                    op: 'delete'
                },
                isArray: false,
            }

        });
    }]);

and I was thinking it will pass screenName as the part of URL. Unfortunately, this does not happen.

The controller code is follows:

            var user = new User();
            user.firstname = $scope.selectedUser.firstName;
            user.lastname = $scope.selectedUser.lastName;
            user.screenName = $scope.selectedUser.screenName;
            user.password1 = $scope.selectedUser.password1;
            user.password2 = $scope.selectedUser.password2;
            user.roles = $scope.selectedUser.roles;
            user.maximalCalories = $scope.selectedUser.maximalCalories;

            user.$update();

Actually it passes:

GET http://localhost:8080/user?op=update HTTP/1.1
Host: localhost:8080
...
Accept: application/json, text/plain, */*
...
Referer: http://localhost:8080/app/index.html

i.e. it passes neither parameters except explicit one.

UDPATE

If I do

    User.$update(
                {
                    firstname : $scope.selectedUser.firstName,
                    lastname : $scope.selectedUser.lastName,
                    screenName : $scope.selectedUser.screenName,
                    password1 : $scope.selectedUser.password1,
                    password2 : $scope.selectedUser.password2,
                    roles : $scope.selectedUser.roles,
                    maximalCalories : $scope.selectedUser.maximalCalories,
                }
            );

I get an exception TypeError: User.$update is not a function

UPDATE 2

Apparently, Angular adds function $update to the object and function update to the class, and if I have object at LHS, it will pass only by POST...

Dims
  • 47,675
  • 117
  • 331
  • 600

2 Answers2

1

You are not passing screenName to resource. Should be:

user.$update({screenName: $scope.selectedUser.screenName});
dfsq
  • 191,768
  • 25
  • 236
  • 258
0

The problem is you have wrong request type for you $resource's udpate object, It should have method PUT instead of GET

update: {
    method: 'PUT', //<--change it 'PUT' from 'GET'
    params: {
        op: 'update'
    },
    isArray: false,
},

Also you had wrong type for delete method, it should have method: 'DELETE' instead of method: 'GET'

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299