2017-03-08 94 views
0

繼火力地堡官方的文檔我的結構我的數據是這樣的:如何使用Angularfire查詢Firebase非規範化數據?

{ 
    "contests": { 
    "1": { 
     "start_time": "2016-01-07T05:46:27 -02:00", 
     "end_time": "2016-08-15T02:00:57 -03:00", 
     "title": "The Best Voice ever!", 
     "category": "music", 
     "videos": { 
     "video_id_44": true 
     }, 
     "users": { 
     "lgraves": true 
     } 
    }, 
    .... 
    }, 
    "users": { 
    "lgraves": { 
     "p_name": "Lorie", 
     "l_name": "Graves", 
     "email": "[email protected]", 
     "location": "Montana", 
     "image_url": "https://randomuser.me/api/portraits/men/85.jpg", 
     "created_time": "2014-12-22T03:40:38 -02:00", 
     "videos": { 
     "video_id_44": true 
     } 
    }, 
    .... 
    }, 
    "videos": { 
    "video_id_44": { 
     "video_url": "www.cdn.video.com/9d0c60af-a069-484e-987f-fad9935dc9a7", 
     "users": { 
     "fhenderson": true 
     } 
    }, ... 
    } 
} 

我的控制器:

var vm = this; 
var contestsRef = firebase.database().ref().child("contests"); 
vm.contests = $firebaseArray(contestsRef);  // synchronize the object with a three-way data binding 

contestsRef.on("value", function(snapshot) { 
     var key = snapshot.key(); /// error: snapshot.key is not a function 
     var childKey = snapshot.val().user; 
     ref.child("users/" + childKey + "/name").once('value', function(snapshot) { 
     }); 
     }); 

當NG-重複上比賽迭代我得到錯誤: 火力警告:異常被拋出用戶回調。 TypeError:snapshot.key不是函數

我的問題是:如何在迭代「比賽」數組時訪問「用戶」和「視頻」,並且在對「比賽」數組進行的任何更改時仍然同步?

回答

1

Javebratt(豪)說,試圖從鍵刪除括號():

var key = snapshot.key; // not snapshot.key() 
+0

的感謝!我現在在下一行(snapshot.val()。user;)上得到undefined。說實話,我不完全明白這些代碼的作用......你有沒有可能獲得ionic1-firebase應用程序的完整示例? – badigard

+0

我GOOGLE了「firebase快照val用戶」,並得到這個:https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#val 嘗試'console.log('快照val',快照.val());'在val()返回的對象中查找要查找的內容。 –