0

Let's say we have a Person $resource that looks like this:

$scope.person = {
  name: 'Max',
  friends: [1, 2, 3]
}

If we Person.save($scope.person) to server, it would send the following parameters:

name: 'Max'
friends: '1'
friends: '2'
friends: '3'

So I will not have access to the array of friends on the server. Only its last element would be accessible.

What is the correct way of saving objects containing arrays to server using $resource?


p.s. I'm aware of a really hacky way, which is renaming friends attribute to friends[], but that's not solving my problem, as I have a lot of these and I can't go with redefining properties back and forth

1 Answer 1

1

From documentation:

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

So you have to use:

Person.save(null, $scope.person)

(first argument is object with url params, second is data to send)

Then your friends array will be available in request body.

Note also that if you have person resource, you can do:

var person = $resource(...);
person.name = 'john';
person.friends = ['friend1' ,'friend2'];
person.$save();
Sign up to request clarification or add additional context in comments.

Comments

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.