0

In my application, I have a form in which the user can add multiple records. When the page is loaded, I need to make a GET request to check the DB for existing records. If there are records that exist, I populate them on the page. No problem there.

The issue I'm having is when the DB has no existing records, the server return a 204 No Content. In the controller, the success function still gets executed but there is no data only the $promise object and $resolved: true.

Here is the code:

Factory:

return $resource (
        https://my.backend/api/records/:id",
        {},
        {
            "getExistingRecords": {
                method: 'GET',
                isArray: false,
                params: {id: '@id'},
                withCredentials: true}
        }
    )

Controller:

function initialize(id){
            alertFactory.getExistingRecords({id: id})
                .$promise
                .then(function (records){
                    if(records){
                        $scope.existingRecords = records;
                    }else {
                        $scope.existingRecords = {};
                    }
                },function(error){
                    Notification.error(error);
                });
        }
initialize(id);

When the server returns "204 No Content" I get this from the console

Console Image

Is the only way to handle this to check for object properties of the records object?

For example:

function initialize(id){
            alertFactory.getExistingRecords({id: id})
                .$promise
                .then(function (records){
                    if(records.recordName){
                        $scope.existingRecords = records;
                    }else {
                        $scope.existingRecords = {};
                    }
                },function(error){
                    Notification.error(error);
                });
        }
initialize(id);

Or am I missing something else?

conbra
  • 13
  • 3
  • Shouldn't `isArray` be `true` in your $resource definition? In the case of an empty response your server could then return an empty array instead of '204 no content'. At least naming the action getExistingRecords() kind of suggests that it should be an array.. – André Hoffmann Jul 20 '16 at 16:29
  • you could always just check to see if records exists at all and it it's length is greater than 0... – theDarse Jul 20 '16 at 16:33

2 Answers2

1

It would have been better if you could get status code with response. There is not direct way for that. But you can workaround with an interceptor:

var resource = $resource(url, {}, {
    get: {
        method: 'GET'
        interceptor: {
            response: function(response) {      
                var result = response.resource;        
                result.$status = response.status;
                return result;
            }
        }
    }                            
});

And now you can:

                if(records.$status === 200){
                    $scope.existingRecords = records;
                }else {
                    $scope.existingRecords = {};
                }
Community
  • 1
  • 1
Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38
0

I think you should return an empty list if there if no registers.