我们正在将应用程序从Parse迁移到Firebase,我想建立一个用户界面,我可以从以下Firebase数据结构中显示学生姓名以及他/她所属的教室。例如,aluck
的课堂名称为10th Grade
。
{
"students": {
"aluck": {
"firstName": "Andrew",
"lastName": "Luck",
"classroom": "-KJlJhyNH6ImKke-qAd-"
},
"bmorino": {
"firstName": "Beth",
"lastName": "Morino",
"classroom": "-KJlJhyNH6ImKke-qAd-"
}
},
"classRooms": {
"-KJlJhyNH6ImKke-qAd-": {
"location": "Down stairs",
"name": "10th Grade",
"students": {
"aluck": true,
"bmorino": true,
}
}
}
}
我的假设是当我使用Firebase JavaScript SDK执行此类操作时
var db = firebase.database();
db.ref("students/aluck").on("value", function(snap) {
// success method will expose the classroom object with it
});
它将公开classRooms
对象,但它不会。
在Parse中,我能够做到
var query = new Parse.Query(Student);
query.include("ClassRoom");
并且它会公开Student
和ClassRoom
个对象。
感谢任何帮助。
答案 0 :(得分:3)
Firebase数据库不支持自动加入学生和教室。您必须使用自己的代码处理此问题。
var db = firebase.database();
db.ref("students/aluck").on("child_added", function(student) {
db.ref("classRooms").child(student.child("classroom").val()).once("value", function(classroom) {
console.log(classroom.val());
});
});
如果您担心此类once
来电的效果,请参阅:Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly