看起来上面的架构可以涵盖您的用例。我建议 Scoreboard 对象的 scores 属性在您的代码中处理(并存储)为对象与数组。
假设 event 属性对于所有 Scoreboard 对象都是唯一的,您可以使用以下内容从 Firebase 中检索它。
const event = 'Volleyball';
const scoreboards = af.database.list('scoreboards', {
query: {
orderByChild: 'event',
equalTo: 'large'
}
});
但是,如果您在对象内有一个唯一键,则可能值得考虑将该键用于 Scoreboard 对象本身,因此记分牌资源如下所示
{
'Volleyball': {
home: 'London',
away: 'Manchester',
scores: {}
},
'Football': {
home: 'London',
away: 'Manchester',
scores: {}
},
...
}
这样做将允许您检索/更新此对象,如下所示。
// Get Scoreboard
const event = 'Volleyball';
const scoreboard = af.database.object('scoreboards/' + event);
// Add a new score to the scores property of Scoreboard
af.database.list('/scoreboards/' + event + '/scores').push({
points: [15, 11],
sets: [2, 1],
timestamp: new Date()
});
值得注意的是,Firebase 实际上并不存储数组;如果你向 Firebase 展示一个数组,它会将它变成一个对象,键是数组的索引。 https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html
根据下面的评论编辑答案
要显示最新存储的值,您可以使用类似下面的方法获取该值。
const event = 'Volleyball';
const scoreboard = af.database.list('/scoreboards/' + event + '/scores', {
query: {
orderByChild: 'timestamp',
limitToLast: 1
}
});
scoreboard.subscribe(list => {
list.forEach(score => {
// Can access the values of the Score object here
console.log(score.points);
});
});