如何使用 Angularfire 查询 Firebase 非规范化数据?
How to query Firebase denormalized data with 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": "loriegraves@zaggle.com",
"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-repeat 进行迭代时出现错误:
FIREBASE 警告:用户回调抛出异常。 TypeError: snapshot.key 不是一个函数
我的问题是:在遍历 'contests' 数组时如何访问 'users' 和 'videos',并且仍然同步对 'contests' 数组所做的任何更改?
Javebratt (Jorge) 说尝试从 key() 中删除括号:
var key = snapshot.key; // not snapshot.key()
按照 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": "loriegraves@zaggle.com",
"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-repeat 进行迭代时出现错误: FIREBASE 警告:用户回调抛出异常。 TypeError: snapshot.key 不是一个函数
我的问题是:在遍历 'contests' 数组时如何访问 'users' 和 'videos',并且仍然同步对 'contests' 数组所做的任何更改?
Javebratt (Jorge) 说尝试从 key() 中删除括号:
var key = snapshot.key; // not snapshot.key()