1

I am trying to consume my spring rest service using angularjs client following this link

Create,update and read parts are working. When I try to delete, its showing this error.

Error: [$resource:badcfg] Error in resource configuration for action get. Expected response to contain an object but got an array (Request: GET http://localhost:8080/SpringRestExample/employee)

Why i am getting GET request in DELETE method?

employee_service.js

'use strict';

App.factory('Employee', ['$resource', function ($resource) {
    return $resource(
            'http://localhost:8080/SpringRestExample/employee/:id', 
            {id: '@employeeId'},
            {
                update: {
                      method: 'PUT' 
                }

            }
    );
}]);

employee_controller.js

'use strict';

App.controller('EmployeeController', ['$scope', 'Employee', function($scope, Employee) {
          var self = this;
          self.employee= new Employee();

          self.employees=[];

          self.fetchAllEmployees = function(){
              self.employees = Employee.query();
          };

          self.createEmployee = function(){
              self.employee.$save(function(){
                  self.fetchAllEmployees();
              });
          };

          self.updateEmployee = function(){
              self.employee.$update(function(){
                  self.fetchAllEmployees();
              });
          };

         self.deleteEmployee = function(identity){
             var employee = Employee.get({employeeId:identity}, function() {
                 employee.$delete(function(){
                      console.log('Deleting employee with id ', identity);
                      self.fetchAllEmployees();
                  });
             });
          };

          self.fetchAllEmployees();

          self.submit = function() {
              if(self.employee.employeeId==null){
                  console.log('Saving New Employee', self.employee);    
                  self.createEmployee();
              }else{
                  console.log('Updating employee with id ', self.employee.employeeId);
                  self.updateEmployee();
                  console.log('Employee updated with id ', self.employee.employeeId);
              }
              self.reset();
          };

          self.edit = function(employeeId){
              console.log('id to be edited', employeeId);
              for(var i = 0; i < self.employees.length; i++){
                  if(self.employees[i].employeeId === employeeId) {
                     self.employee = angular.copy(self.employees[i]);
                     break;
                  }
              }
          };

          self.remove = function(employeeId){
              console.log('id to be deleted', employeeId);
              if(self.employee.employeeId === employeeId) {//If it is the one shown on screen, reset screen
                 self.reset();
              }
              self.deleteEmployee(employeeId);
          };


          self.reset = function(){
              self.employee= new Employee();
              $scope.myForm.$setPristine(); //reset Form
          };

      }]);
6
  • please someone help me. Commented Oct 12, 2016 at 10:16
  • Sounds like your backend is returning an array for the .get request. Nothing to do with DELETE. Check the response data for http://localhost:8080/SpringRestExample/employee/someEmployeeId Commented Oct 12, 2016 at 11:03
  • @Phil Thanks for the suggestion. Commented Oct 12, 2016 at 11:15
  • @Phil the url will return employee details(id,firstname,lastname and age) of the employeeId. Commented Oct 12, 2016 at 12:33
  • That's not what I meant. Pick a valid employee ID (let's say it's "1") and use curl or similar to request http://localhost:8080/SpringRestExample/employee/1. If the response starts with [ and not {, then it is returning an array where Angular is expecting an object Commented Oct 12, 2016 at 23:36

1 Answer 1

1

Your issue could be when you call Employee.get({employeeId:identity}, ...) prior to deleting the employee. This will load the employee before deletion and it will do a GET request on 'http://localhost:8080/SpringRestExample/employee/:id'.

For this query to work properly, you need to provide id, which you haven't done, so it might just be leaving out that part of the URL. You provided employeeId, which is only used for mapping the id parameter to the Employee objects. Try replacing the query above with {id: identity}.

Sign up to request clarification or add additional context in comments.

3 Comments

@Phil so i have got some other issue as you suggested in the first comment?
@Phil I think that is only for mapping to the Employee.employeeId property.
@lyschoening you are correct. From the documentation ~ "If the parameter value is prefixed with @, then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling a "non-GET" action method)."

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.