0

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

7
  • You are returning an array of errors. You have set isArray:false and data must be in an array format thus response to contain an object but got an array Commented May 30, 2015 at 11:59
  • I thought that "bad criteria" would return 400 status code (bad request). Commented May 30, 2015 at 12:00
  • @Wédney Yuri - I Thought so as well, but if you check my updated question, there is a picture of header. Commented May 30, 2015 at 13:28
  • @Satpal - I don't think so, because Header's status code is 204 No Content. Commented May 30, 2015 at 13:28
  • @Sami you can change the server code? I think the most correct would it return 400 (bad request) in the case of "bad criteria". Commented May 30, 2015 at 13:33

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.