Pregunta

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.

¿Fue útil?

Solución

I'm the creator of Restangular. Flim is right :).

In the promise then you get the object returned from the server :)

firstAccount.post("Buildings", myBuilding).then(function(addedBuilding) {
   console.log("id", addedBuilding.id);
 }, function() {
  console.log("There was an error saving");
 });

Thanks!

Otros consejos

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.

firstAccount.post("Buildings", myBuilding).then(function(resp) {
  console.log(resp.id);  // if the JSON obj has the id as part of the response
});

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 like http://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 be ITypeB. Then our restangular will not be able to use a standard POST of ITypeA and expect a response of ITypeB, so the following code won't be correct because restangular would expected to receive a response of type ITypeA (same as the one posted).

public postAnObject(objectToPost: models.ITypeA): ng.IPromise<models.ITypeB> {
     return this.restangular.all("objects")
            .post<models.ITypeA>(objectToPost)
            .then((responseObject: models.ITypeB) => {
                return responseObject;
            }, (restangularError: any) => {
                throw "Error adding object. Status: " + restangularError.status;
            });
}

That can be resolved by using a customPOST, so the code above would be correct like this:

public postAnObject(objectToPost: models.ITypeA): ng.IPromise<models.ITypeB> {
     return this.restangular.all("objects")
            .customPOST(objectToPost)
            .then((restangularizedObjectTypeB: restangular.IElement) => {
                return restangularizedObjectTypeB.plain();
            }, (restangularError: any) => {
                throw "Error adding object. Status: " + restangularError.status;
            });
}

There are a few things to notice, in summary:

  1. Restangular can get the response object in the successfull callback (the then part)
  2. If using the method .post(objectA) restangular will expect, if any, a successfull callback with a response of the same type as the objectA.
  3. If you want to post an objectA but get a response objectB (different types) then use the method .customPOST(objectA)
  4. IMPORTANT: the response is actually a "restangularized" object that wraps the "real" response object. That means that the response contains some restangular methods. If you simply want the response object call the method .plain() on the response as shown in my second example where the response is not actually a ITypeB object but a restangular.IElement
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top