Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error handling with Restangular

I'm trying to retrieve a JSON object from my backend using Restangular.

1) This works great, but I don't know how I can handle errors because there is no callback/promise function :

meals = restAngular.all('meal').getList({date: selectedDate}).$object;

The following with the promises doesn't work :

restAngular.all('meal').getList({date: selectedDate}).then(function(newMeals){
    console.log(newMeals);
    meals = newMeals;
});

I got a Restangular object in my "newMeals" object instead of my Json object. Something like :

addRestangularMethod: function (){var g=arguments,h=a?u:this;
all: function () { [native code] }
allUrl: function () { [native code] }
clone: function () { [native code] }
customDELETE: function () { [native code] }
etc..

How can I get my JSON "meals", using promises and being able to handle potential errors ?

2) Additional question : is Restangular still a good choice since Angularjs 1.2 and his better new $resource is implemented ?

Thanks a lot

like image 746
GSP59 Avatar asked Jan 22 '26 00:01

GSP59


1 Answers

You can do something like this:

restAngular.all('meal').getList({date: selectedDate}).then(function(newMeals){
    console.log(newMeals);
    meals = newMeals;
}, function(response) {
    console.log("Error with status code", response.status);
});
like image 78
Mircea Georgescu Avatar answered Jan 24 '26 16:01

Mircea Georgescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!