![](/img/trans.png)
[英]Firebase - Data structure issue for extracting an object from nested structure
[英]Storing data from a nested firebase object
我正在使用一个考勤Web应用程序,该应用程序每月显示一次信息。 这是我的firebase数据库结构 : Firebase数据库结构
要遍历数据,我使用以下方法:
for(i = 0; i<months.length;i++){
console.log(months[i]);
var datesRef = firebase.database().ref('students/' + currentStudentSap + '/attendance/' + currentSem + '/' + months[i]);
datesRef.on('value',function(snapshot){
var dates = snapshot.val();
var date = Object.keys(dates);
for(j=0;j<date.length;j++){
console.log(date[j]);
var lectureRef = firebase.database().ref('students/' + currentStudentSap + '/attendance/' + currentSem + '/' + months[i] + "/" + date[j]);
lectureRef.on('value',function(snapshot){
var lectures = snapshot.val();
console.log(lectures, typeof(lectures));
});
}
});
}
这是我在控制台上得到的: 控制台数据
我现在要做的是将这些统计信息保存在某种数据结构中,以便可以使用它们来生成图形图表。 例如:“ COSTtheory”是一个主题,我想计算每个月进行的讲座总数和当前讲座总数,并以HTML形式显示按月统计(例如出席率)。
我不知道您是如何在Firebase中插入数据的,但是我不会像您一样遍历它。 取而代之的是,我将增加每次插入数据时需要报告的月度总计,这样我就可以显示实时统计信息(实时数据库的用途)。 我将使用具有云功能的Firebase数据库事务来实现此目的。 事务确保数据一致性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.