Calling another $scope.function returns undefined


#1

Hi, i’m using Ionic ngStorage and Azure Mobile Services to persist data. So i have the following:
My app has a search button where i pass as parameter the type and city, it searchs the cloud and returns the matchs as an array of objects. The results can be stored in the phone as favorites (so the person can’t have to access the cloud everytime), and i need to compare the cloud and the local storage so if it is already in the favs, the person can’t press the button to fav it again, here is my code so far.

THE SEARCH FUNCTION:
$scope.buscar = function(tipo, cidade){ var favs = $localStorage.favoritos; console.log(favs); var userid = $localStorage.idusuario; if(cidade != undefined){ var cid = cidade.toUpperCase(); } else{ var cid = ""; }; $ionicLoading.show({ template: '<ion-spinner icon="bubbles"></ion-spinner><br/>Buscando Locais!' }); if(tipo != "" && cid != ""){ table.where({tipo: tipo, cidade: cid}).read({user: userid}).done(function(locais){ if(locais.length == 0) { $ionicPopup.alert({ title: 'Nenhum local encontrado', template: 'Verifique se digitou o nome do local corretamente.' }); }; $scope.locais = locais; $scope.checarfavoritos($scope.locais); $ionicLoading.hide(); }, function(error){ console.log(error.message); $ionicLoading.hide(); }); };

AND HERE IS MY FUNCTION THAT COMPARES THE FAVS AND THE CLOUD
$scope.checarfavoritos = function(locais){ var favs = $localStorage.favoritos; if(favs.length !== null || favs.length !== undefined) { for(var i = 0; i < locais.length; i++) { for(var j = 0; j < favs.length; j++) { if(locais[i].id == favs[j].id) { document.getElementById(favs[j].id.toString()).disabled = true; }; }; }; }; };

but it gives a error Uncaught Error: TypeError: Cannot read property ‘length’ of undefined in MobileServices.Web-1.2.7.min.js

Any ideas of what i’m doing wrong?