I have the following AngularJS controller setup to grab information from an outside API. The problem is there could be a situation where the mobile device does not have or has a terrible internet connection. I’m using cordovaNetwork to see if the device is online but it appears to not be working.
I am getting the following error in console: TypeError: Cannot read property ‘type’ of undefined
Can someone tell me what I’m missing:
.controller('LennoxProductCtrl', function($scope, $ionicPopup, $cordovaNetwork) {
if($cordovaNetwork.isOnline()) {
$scope.loading = true;
$.ajax({
url: "https://api.lennox.com/v1/hSPgMEU/categories",
dataType: 'json',
headers: { Accept: 'application/json' },
type: 'GET',
success: function(data) {
$scope.loading = false;
$scope.$apply(function() {
console.log(data.Category);
$scope.categories = data.Category;
});
localStorage.setItem("lennoxProducts", JSON.stringify(data.Category));
},
error: function (data) {
if(localStorage.getItem("lennoxProducts") !== undefined) {
$scope.categories = JSON.parse(localStorage.getItem("lennoxProducts"));
}
$scope.loading = false;
}
});
} else {
$ionicPopup.alert({
title: 'Error',
content: 'Check Your Network Connection'
});
}
})