2016-03-15 51 views
-1

我有一個應用程序,其中有一些用於過濾數據的下拉列表。一旦我將json移動到firebase,我無法檢索這些下拉列表中的數據。 app當應用程序遷移到firebase時數據未加載

的FOLL是控制檯錯誤:

TypeError: Cannot read property 'tags' of undefined 
at classifieds.ctr.js:135 
at Object.forEach (angular.js:321) 
at getTags (classifieds.ctr.js:134) 
at classifieds.ctr.js:20 
at processQueue (angular.js:15552) 
at angular.js:15568 
at Scope.$eval (angular.js:16820) 
at Scope.$digest (angular.js:16636) 
at angular.js:16859 
at completeOutstandingRequest (angular.js:5804) 

的FOLL是代碼:

angular 
.module("ngClassifieds") 
.controller("classifiedsCtrl", function($scope, $state, $http, classifiedsFactory, $mdSidenav, $mdToast, $mdDialog) { 


    $scope.classifieds = classifiedsFactory.ref; 
    $scope.classifieds.$loaded().then(function(classifieds) { 
     $scope.tags = getTags(classifieds); // call the getTags method below 
     $scope.books = getBooks(classifieds); // call the getBooks method below 
     $scope.authors = getAuthors(classifieds); // call the getAuthors method below 
     $scope.order = ""; //for sorting in asc or desc order 
    }); 

的FOLL是getTags方法:

function getTags(classifieds) { 

     var tags = []; 
     angular.forEach(classifieds, function(item) { 
      angular.forEach(item.meta.tags, function(tag) { 

       tags.push(tag); 
      }); 

     }); 

     return _.uniq(tags); 

    } 

的FOLL是在Firefox中的錯誤: 無法訪問的代碼後R返回聲明

Error: item.meta is undefined 
getTags/<@http://localhost:8080/components/classifieds/classifieds.ctr.js:135:5 
[email protected]://localhost:8080/node_modules/angular/angular.js:321:11 
[email protected]://localhost:8080/components/classifieds/classifieds.ctr.js:134:4 
@http://localhost:8080/components/classifieds/classifieds.ctr.js:20:18 
[email protected]://localhost:8080/node_modules/angular/angular.js:15552:28 
scheduleProcessQueue/<@http://localhost:8080/node_modules/angular/angular.js:15568:27 
$RootScopeProvider/this.$get</[email protected]://localhost:8080/node_modules/angular/angular.js:16820:16 
$RootScopeProvider/this.$get</[email protected]://localhost:8080/node_modules/angular/angular.js:16636:15 
$RootScopeProvider/this.$get</Scope.prototype.$evalAsync/<@http://localhost:8080/node_modules/angular/angular.js:16859:15 
[email protected]://localhost:8080/node_modules/angular/angular.js:5804:7 
Browser/self.defer/timeoutId<@http://localhost:8080/node_modules/angular/angular.js:6081:7 

編號真的很感激,如果你能幫助我在那裏IM布萊恩錯在這裏。謝謝

+0

請參閱[使用緩存加入用戶記錄](https://gist.github.com/katowulf/f78d4a224c06a643ddfa)和[使用$ extend過濾記錄](https://gist.github.com/katowulf/bee266e31aa60cb0eed6) – Kato

回答

1

AngularFire $loaded()事件適用於一組非常有限的特定用例,您希望以不同於其他數據的方式處理最初加載的數據。這不是這種情況,所以你不應該使用$loaded()

如果你有在你的數據庫,你想在視圖中顯示的標籤列表,你應該得到的標記,名單爲$firebaseArray()

var tagsRef = ref.child('tags'); 
$scope.tags = $firebaseArray(tagsRef); 

這將加載標籤和自動更新加載後的視圖。更好的是,它還將監視數據庫的更改並在數據發生更改時更新視圖。

$scope.books$scope.authors做同樣的事情,你會有一個更容易的時間。設置三個$firebaseArray()對象而不是一個單一的$firebaseObject()可能會違反直覺。但是這兩種方式的成本幾乎相同。

+0

Frank,但是如果我要實現這個建議,我該如何調用getTags()方法?這裏的coz標籤指的是一個新定義的數組,它只存儲json中每個對象/單詞上'tags'屬性的所有標籤的一個實例。 – Nosail

+0

啊,所以你試圖過濾數據。這對於'$ loaded()'也是可能的,但仍然不是一個好的用例。您需要擴展'$ firebaseArray()'類。可能類似於Kato(AngularFire的主要作者)的回答:http://stackoverflow.com/questions/32359624/joining-angularfire-paths-on-a-value-key-is-not-working-merging- user-profiles-i –

+0

謝謝弗蘭克,我沒有能夠通過你引用的帖子中的所有消息,但這是我如何實現它,並且所有數據現在都通過了。 'function getTags(classifieds){ \t \t \t var tags = []; \t \t \t angular.forEach(分類,函數(項目){ \t \t \t \t如果(item.meta){ \t \t \t \t \t angular.forEach(item.meta。標籤,功能(標籤){ \t \t \t \t \t tags.push(tag); \t \t \t \t \t}); \t \t \t \t} \t \t \t}); \t \t \t \t \t \t return _.uniq(tags); \t \t \t \t \t} – Nosail

相關問題