restangular icon indicating copy to clipboard operation
restangular copied to clipboard

POST to undefined path

Open gabzim opened this issue 12 years ago • 6 comments

In order to take advantage of the extended restangular model I have a code looking like this:

$scope.things = Restangular.all('things').getList().$object;

$scope.newThing = Restangular.one('things');

$scope.saveThing = function () {
  $scope.newThing.save().then( function(createdThing) {
     $scope.things.push(createdThing)
  });
}

What I do there is once creation is successful, I add the newThing to the things array that I fetched upon controller creation, so I avoid asking for all things again to refresh the list. So far so good, the problem arises when I wish to edit this newly added thing:

$scope.saveChanges = function(thing) {
  thing.save().then(function(){
    console.log('Thing saved');
  })
}

if i wish to update the newThing that I just posted to the server, the http method now should be PUT, and it is, but to the url:

http://localhost:8080/api/things/undefined/the-id-of-the-thing

I am guessing that this is because the save() method returns a subresource of Thing (and i created the thing doing Restangular.one('things') without passing an id ), but this is a bit unexpected in my case. All I want to do is create a Thing, then post it, then once I get the Thing back from the server with its id, update it without restangular adding that undefined in the middle.

gabzim avatar Jul 08 '14 13:07 gabzim

This is definetly a bug.

Thanks for the report. We'll take a look :).

mgonto avatar Aug 17 '14 22:08 mgonto

Could you please send me a plunkr or jsfiddle with the error? You can use http://node-backend-todo.herokuapp.com/api/todos service.

Thanks!

mgonto avatar Aug 17 '14 23:08 mgonto

I have encountered this bug as well. Attempting to interact with a freshly returned Restangularized element from a POST produces a 404.

yes it is a Restangular object Object {id: 2, name: "Taskerdoodle", completed: false, route: "", getRestangularUrl: function…} script.js:44
PATCH http://node-backend-todo.herokuapp.com/api/todos//2/ 404 (Not Found) node-backend-todo.herokuapp.com/api/todos//2/:1

I put together a plunkr here that reproduces the problem: http://plnkr.co/edit/eoUiIo?p=preview

twhitman avatar Sep 05 '14 07:09 twhitman

Same here.

This plunkr reproduces the issue too: http://embed.plnkr.co/OnSHvdFoDC6AXTwAArIw/preview

timvanginderen avatar Sep 12 '14 01:09 timvanginderen

+1

Blaze34 avatar Jan 09 '15 08:01 Blaze34

Using

$newObject = Restangular.all('todos').post({objectToPost});

as per documentation solves the issue. Undefined appears because of wrong usage of one() that needs a second parameter id. Because of the fact it's not present, you get those undefined values.

grabbou avatar Feb 21 '15 14:02 grabbou