Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Response from Restangular POST

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.

like image 689
Hass Avatar asked Jun 16 '13 17:06

Hass


2 Answers

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!

like image 72
mgonto Avatar answered Nov 05 '22 01:11

mgonto


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
});
like image 34
Foo L Avatar answered Nov 05 '22 02:11

Foo L