POST to undefined path
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.
This is definetly a bug.
Thanks for the report. We'll take a look :).
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!
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
Same here.
This plunkr reproduces the issue too: http://embed.plnkr.co/OnSHvdFoDC6AXTwAArIw/preview
+1
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.