How do I get the response object after I send a Restangular POST?
firstAccount.post("Buildings", myBuilding).then(function() {
console.log("Object saved OK");
}, function() {
console.log("There was an error saving");
});
I'm trying to get the new object id.
Thanks.
I'm the creator of Restangular. Flim is right :).
In the promise then you get the object returned from the server :)
Thanks!
A restangular POST will expect the same object in response than the one posted.
This is clearly seen with typescript definitions. Suppose we have a method that is going to receive an object of type
ITypeA
and it's going to POST it in a url likehttp://whatever/api/objects
. Suppose that the REST api returns a 201 and also a json with the response object that can be the same OR DIFFERENT. In our case suppose the type returned would beITypeB
. Then our restangular will not be able to use a standard POST ofITypeA
and expect a response ofITypeB
, so the following code won't be correct because restangular would expected to receive a response of typeITypeA
(same as the one posted).That can be resolved by using a customPOST, so the code above would be correct like this:
There are a few things to notice, in summary:
then
part).post(objectA)
restangular will expect, if any, a successfull callback with a response of the same type as the objectA..customPOST(objectA)
.plain()
on the response as shown in my second example where the response is not actually aITypeB
object but arestangular.IElement
I haven't worked with Restangular directly, but your POST probably needs to return a JSON object with the ID. Then, your success function has to accept it as a parameter.