0

At first I had this, but got error Expected response to contain an object but got an array:

angular.module('homeModule').factory("TodosFactory", ['$resource', function ($resource) {
return $resource('/api/todos/:todoId', {
    todoId: '@_id'
});
}]);

So I changed to this:

angular.module('homeModule').factory("TodosFactory", ['$resource', function ($resource) {
return $resource('/api/todos/:todoId', {
    todoId: '@_id'
}, {'save': {method: 'POST', isArray:true}}); // this line is added
}]);

I am saving an item to database, and returning list of objects as a result. An JSON array is returned, but I get this error.

Any suggestions?

Jaanus
  • 16,161
  • 49
  • 147
  • 202

1 Answers1

1

The isArray property is for both the request and the response. See AngularJs $resource use of isArray for both request and response.

It's getting an error because the request is not an array. You could wrap it in an array, and then pull it out server side.

Community
  • 1
  • 1
dnc253
  • 39,967
  • 41
  • 141
  • 157
  • Ok, possibly will solve the problem, but I already wrote the code, so that that the item that is added to database, will be returned, not all the elements, so it will respect the REST architecture. – Jaanus Nov 26 '13 at 09:36