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
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?