Sami
Sami

Reputation: 2331

Catching errors in AngularJS when using $resource

Searching product is working fine when product is found, but if user search with letters, or bad criteria, nothing happens. I can see the error message in JavaScript-console and in Header there is Status Code 204, no Content. I think that Angular is just working badly, when empty object is coming back. How can I tell an user that there is no products with his/her criteria, cause I can't catch the error message at all at the moment. What is the correct and best solution to handle this? Catching errors or solving that result was empty and showing an error message in HTML-page?

Service:

return $resource(

                    'http://localhost:8080/RMAServer/webresources/com.rako.entity.jdeserials/:id',
                    {},
                    {
                        get: { method: 'GET',isArray:false, params: {id: '@serial'} },
                        update: { method: 'PUT', params: {id: '@serial'} }
                    });

Controller

    //Searching product with serial number/LOTN
                $scope.searchProduct = function () {
                    $scope.serials = lotnSvc.get({id: $scope.serial}).$promise.then(
                        function (data) {
                            var litm = data.litm;

                            productSvc.get({id: litm}, function (product) {
                            $scope.product = product;
                            getBrands();
                        },function(error){
                      alert('ERROR when searching product'); 
                      console.log("error---->" + error);
                    });
                    },function(error){
                      alert('ERROR when searching product'); 
                      console.log("error---->" + error); 
                    });

                };

Error message in javaScript console

Error: [$resource:badcfg] Error in resource configuration for action `get`. Expected response to contain an object but got an array
http://errors.angularjs.org/1.3.15/$resource/badcfg?p0=get&p1=object&p2=array
at REGEX_STRING_REGEXP 

Status code in Header

Status code in header after GET

Upvotes: 0

Views: 1314

Answers (1)

MaximSadym
MaximSadym

Reputation: 71

Just try to send the success and the error handlers as second and third parameters to "get" function instead of using promises. There was the same problem: How to handle $resource service errors in AngularJS

Upvotes: 1

Related Questions